I would like to select multiple values in where clause but it is not selecting anything.
This is the select query I have:
'SELECT * FROM table WHERE id IN (4, 5) ORDER BY id desc'
what am I missing?
Based on your comments, the reason that the query is failing is because the column is a varchar and you are using int values in your IN clause. MySQL does not convert the type if you quote the numbers then your query will work with varchar
http://dev.mysql.com/doc/refman/5.0/en/type-conversion.html
I would imagine your table has no data with id = 4 or id = 5.
Try
SELECT * FROM table WHERE id = 4
Does that return anything either? I would bet no.
Why not just 'SELECT * FROM table WHERE (id = '4' OR id= '5') ORDER BY id desc'
Related
This is my query to get filter data from user table where category will fetch from another table.
SELECT * FROM jobfia_users
WHERE country_id='4'
and user_id IN (SELECT worker_id
FROM jobfia_worker_skills
WHERE skill_id = '42'
)
This is not giving any error, but not return any row also.
while there are lots of records are available in table using this filter.
Can any one help please ?
Additionally to the quotes surrounding your INT ids, your query will be better expressed like this :
SELECT u.*
FROM jobfia_users u
INNER JOIN jobfia_worker_skills ws
ON ws.worker_id=u.user_id AND ws.skill_id = 42
WHERE u.country_id=4
If your country_id and skill_id are int type, remove ' around values.
SELECT * FROM jobfia_users
WHERE country_id=4
and user_id IN (SELECT worker_id
FROM jobfia_worker_skills
WHERE skill_id = 42
)
Tested your code with and without '' and it works. Make sure you have data and you did not misspell some column name.
Maybe you have collision of some column names. Try to use this syntax:
\`table_name\`.\`column_name\`
Code:
SELECT *
FROM `jobfia_users`
WHERE `jobfia_users`.`country_id`='4'
AND `jobfia_users`.`user_id` IN (SELECT `jobfia_worker_skills`.`worker_id`
FROM `jobfia_worker_skills`
WHERE `jobfia_worker_skills`.`skill_id` = '42')
$result_array= select * from table where id = 5;
I am getting this result_array from another function.
And in my function, I want to filter out the result based on the returned array.
Is something like this
select * from table where * IN result_array AND type=type1 ORDER BY timestamp
possible?
This query is giving me an error
Unknown column 'Array' in 'where clause
You can do it like as:
"select * from table where column_name(like user_id) IN (select user_id from table where id = 5) AND type=type1 ORDER BY timestamp"
IN always accept an array of values.
For example we have two tables dealers and dealers_login.
We can filter out dealer login details like :
SELECT * FROM dealer_login WHERE id IN (SELECT dealer_id FROM dealers)
Thanks
I have a Table Which has a column test_id i need To Fetch Data Which is not Equal To t-0105,t-0120,t-044,t-063,t-064,t-068,t-072 but Query Fetching all data.
select * from test_name where test_type='p' and test_id NOT IN ('t-0105,t-0120,t-044,t-063,t-064,t-068,t-072') and test_list='tt' and test_name like '%aptitude%' and published=1 order by id asc
You put the single quote ' as wrong. In your query IN condition takes all values as a single string. you should add it as shown below.
select * from test_name where test_type='p' and test_id NOT IN ('t-0105','t-0120','t-044','t-063','t-064','t-068','t-072') and test_list='tt' and test_name like '%aptitude%' and published=1 order by id asc
I have 1 mySQL table. Table entries are:
I want to show a list under 2 conditions. First the highest right_answers and also depending on lowest time_spent
I used this sql code to retrieve the list
mysql_query("SELECT * FROM `table_name` WHERE `contest_id` = '2' ORDER BY right_answers desc,time_spent")
I am getting this output:
My expected output shoulb be:
I tried many times with different queries but the result is still wrong. Could you give me some solutions?
Thanks in advance!
I guess your time_spent column has a VARCHAR(x) datatype, not a numeric datatype. So, MySQL is ordering the values in alphabetical order like this.
1
10
259
46
5
6
7894
9
Your best repair of this problem is to change the datatype of that column to INT. (Also repair the right_answers column.)
ALTER TABLE table_name CHANGE COLUMN time_spent time_spent INT NOT NULL DEFAULT '0'
ALTER TABLE table_name CHANGE COLUMN right_answers right_answers INT NOT NULL DEFAULT '0'
Then your ORDER BY operation will work numerically and give you expected results.
Your second best repair is to coerce the data to numeric in your ORDER BY clause, like this:
ORDER BY 0+right_answers DESC, 0+time_spent ASC
You try this way,I am trying its working
mysql_query("SELECT * FROM table_name WHERE contest_id = '2' ORDER BY table_name.right_answers desc,time_spent ASC");
Try something like this:
SELECT *
FROM `table_name` WHERE `contest_id` = '2'
ORDER BY SUBSTRING( time_spent
FROM 1
FOR 1 ) ASC , right_answers DESC
Try this
mysql_query("SELECT * FROM table_name WHERE contest_id = '2' and id in (SELECT ID FROM table_name ORDER BY right_answers DESC) ORDER BY time_spent ASC")
interchange column names and try again.
I' am storing the profile friends ID in a string format comma delimited. When the run the Query, its gives 1 record whereas it should return with 2 records.
MySQL Query
SELECT * FROM PROFILES WHERE profile_id IN(SELECT profile_friends FROM PROFILES WHERE profile_id = '1')
Which gives this result (The results should be 2 records NOT 1 Record)
When I run the following Query, gives me two ID's that are in a profile_friends field.
SELECT profile_friends FROM PROFILES WHERE profile_id = '1'
Please use following Query
SELECT * FROM PROFILES WHERE FIND_IN_SET(profile_id,(SELECT profile_friends FROM PROFILES WHERE profile_id = '1'))
IN clause only use when you search value from integer field
But your field(profile_friends) is string so following clause you need to use.
FIND_IN_SET