mysql

MYSQL - Query to check against other table (hard to explain...)

I have a query that gets a list of emails who have subscribed for a newsletter trial which lasts 30 days.. $thirty = time() - 3024000; SELECT c.email FROM tbl_clients AS c JOIN tbl_clientoptions AS o ON o.client = c.id WHERE o.option = 'newsletter' AND c.datecreated > $thirty What I want to do is do a check in that same q...

Help Forming An SQL Query That Selects The Max Difference Of Two Fields

I'm trying to select a record with the most effective votes. Each record has an id, the number of upvotes (int) and the number of downvotes (int) in a MySQL database. I know basic update, select, insert queries but I'm unsure of how to form a query that looks something like: SELECT * FROM topics WHERE MAX(topic.upvotes - topic.dow...

Unique Key in MySql

I have a table with four Columns: Col1, Col2, Col3, and Col4. Col1, Col2, Col3 are strings, and Col4 is a integer primary key with Auto Increment. Now my requirement is to have unique combination of Col2 and Col3. I mean to say like. Insert into table(Col1, Col2, Col3) Values ('val1', 'val2', 'val3'); Insert into table(Col1, Col2, Col...

PHP - get MySQL query results as their native data type?

I've tried fetching MySQL query results using mysql_fetch_row() mysql_result() and numeric values are being returned as strings. Is there any way to fetch the data as its datatype stored in the table? The application will be querying many different queries so I will be unable to cast the values as the intended datatype on a 1 by 1 basi...

define mysql indexing

What is indexing? What is full text? I know the answers to both questions, but I can't expose those answers in the exact way to an interviewer: indexing means something like index in book fulltext means for search string Can please give me very simple definition for each of these questions? ...

Preventing entire JOINed MYSQL query from failing when one field is missing within a WHERE clause

I am doing a couple of joins with a variable in the WHERE clause. I'm not sure if I am doing everything as efficiently as I could, or even using the best practices but my issue is that half my tables have data for when tableC.type=500, and the other half don't resulting in the entire query failing. SELECT tableA.value1 , tableB.value2, ...

optional search parameters in sql query and rows with null values

Ok here is my problem : Before i start the description, let me to tell you that I have googled up a lot and I am posting this question for a good optimal solution :) i am building a rest service on WCF to get userProfiles... the user can filter userProfiles by giving something like userProfiles?location=London now i have the following...

mysqldump from a query

How do we take a mysql dump for table from a query I need something like this.. `mysqldump -uroot -pxxxx mydb "select * from table where name='1';" > /tmp/a Thanks ...

Create automatically table that is composed by its primary key and 2 other foreign keys

I have table A with a primary id, a table B with a primary key id also and another table C with a primary key id and rows Aid and Bid where Aid and Bid are foreign keys of C and the primary keys of A and B respectively. One way of populating these DB would be populating table A, table B and table C separately. I would like to know if...

finding the number of 1's in my table in order

I have a table of customers with a 1 recorded against their customerid on different dates. I would like to find the sum of the 1's recorded in descending order. I'm using MySQL and php Thanks ...

PHP Moving mySQL Tree Node

I am having trouble trying to move sub nodes or parent nodes up or down... not that good at math. CREATE TABLE IF NOT EXISTS `pages` ( `page-id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, page-left mediumint(8) unsigned NOT NULL, page-right smallint(8) unsigned NOT NULL, page-title text NOT NULL, page-content te...

Could you help me write a proper query in rails for accessing the following information?

@workname = [] @recos = [] @bas = [] if current_user.recommendations.size != 0 current_user.recommendations.each do |r| if r.work_type == 'J' @job = Job.find_by_id(r.work_id) @workname.push "#{@job.title} at #{@job.company.name}" else @qualification = Qual...

How do I ensure data consistency in this concurrent situation?

The problem is this: I have multiple competing threads (100+) that need to access one database table Each thread will pass a String name - where that name exists in the table, the database should return the id for the row, where the name doesn't already exist, the name should be inserted and the id returned. There can only ever be one ...

Mysql query problem

I have a problem with (for me to complicated) MySql query. Okay, here is what I need to do: First I need to check messages that some specific user received $mid=$_SESSION['user']; $stat1=mysql_query("SELECT id, fromid, toid, subject FROM messages WHERE toid = '".$mid."' AND subject != 'not readed' GROUP BY fromid ") or die(mysql_err...

Createing a new Index in SQL when current records don't meet that index

Hey all- I'd like to add an index to a table that already contains data. I know that there a few records currently in the table that are not unique with this new index. Clearly, MySQL won't let me add the index until all of them are. I need a query to identify the rows which currently have the same index. I can then delete or modif...

How to get a table creation script in MySQL Workbench?

I am rolling back to MySQL GUI Tools' MySQL Query Browser since I can't find the shortcut to get a table's creation script in MySQL Workbench. ...

PDO in mysql performance

Recently I was going through a blog and noticed some points about using PDO in mysql and it changed my view about the PDO. The points are : * native prepared statements cannot take advantage of the query cache, resulting in lower performance. * native prepared statements cannot execute certains types of queries, like "SHOW TABLES" * nat...

MySql transqactions with dependent queries

Hi everybody, I really hope you can help! I use the following function to send a message in my PHP/MySql application: public function sendMail($sender_id, $recipient_id, $subject, $message) { $q = "INSERT INTO MAIL_MESSAGE (subject, message, date) VALUES ('$subject', '$message', NOW() )"; $s = mysql_query($q); i...

Application Code Redesign to reduce no. of Database Hits from Performance Perspective

Scenario I want to parse a large CSV file and inserts data into the database, csv file has approximately 100K rows of data. Currently I am using fgetcsv to parse through the file row by row and insert data into Database and so right now I am hitting database for each line of data present in csv file so currently database hit count is ...

How to make a remote connection to a MySQL Database Server?

Hello: I am trying to connect to a MySQL database server (remote), but I can't. I am using an user with grant privileges (not root user). The error message is the following: Can't obtain database list from the server. Access denied for user 'myuser'@'mypcname' (using password: YES) "myuser" is an user I created with grant access. This...