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
Related
getting query empty with this query. butt data is there in database
select * from applies_jobs where appid like '%RAD-0001%' and job_id IN ('41,48,49') order by id asc
if i use only in or like query it work fine.
try this:
select * from applies_jobs where appid like '%RAD-0001%' and job_id IN ('41','48','49') order by id asc
in the IN use separated values
try this, removed single qoutes for integer value
select * from applies_jobs where appid like '%RAD-0001%' and job_id IN (41,48,49) order by id asc
I have 2 tables, names and phones I did this for the query
$result = mysqli_query($mysqli, "SELECT * FROM names ORDER BY fname ASC
RIGHT JOIN phones ON phones.id=names.phone_id"
);
I got $result as false. My names table has a column named phone_id and it's a PK of phones's id, like so
names
- phone_id (FK)
phones
- id (PK)
What's wrong with my sql above?
The syntax should go like this:
SELECT *
FROM names
RIGHT JOIN phones ON phones.id = names.phone_id
ORDER BY fname ASC
The ORDER BY had to be moved to the end.
Order by should be the last part of your query
SELECT *
FROM names n
RIGHT JOIN phones p ON p.id=n.phone_id
ORDER BY fname ASC
Start using alias names to make the query more readable
You should use ORDER BY as last clause of your query. Becasue order by i.e. sorting works at last after fetch.
SELECT *
FROM names as n
RIGHT JOIN phones as p ON p.id=n.phone_id
ORDER BY fname ASC
To see more abour ORDER BY you can check the manual link
$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 'book_history' with a field 'status'
So there are three values for the field 'status' => 0,1,2
Now i want to query it in orderby - I know order by asc and order by desc is there.
But how i really want is
select * from book_history order by status 1,0,2
I checked with order by then also. But I was not able to make my query.
So the final output will be - first it should list the status=1, then status=0 and then status='2'
Any help.
Thanks,
Kimz
You can use FIELD() function
SELECT
*
FROM
book_history
ORDER BY FIELD(`status`, 1, 0, 2)
Use below query to force order in query:
select * from book_history
ORDER BY FIELD (status,1,0,2);
For more information if there is more values and you keep your values on top and rest values after that then you can use below query:
select * from book_history
ORDER BY FIELD (status,2,0,1) desc;
You can use ORDER BY FIELD:
SELECT * FROM book_history ORDER BY FIELD(status, 1,0,2)
Use a case expression to get an order key:
select *
from book_history
order by
case status
when 1 then 100 -- any small value would do here
when 0 then 200 -- any medium value would do here
when 2 then 300 -- any big value would do here
end;
Another alternative with UNION:
select * from book_history where status = 1
UNION
select * from book_history where status = 0
UNION
select * from book_history where status = 2
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'