I have a sample SQL statement that says:
SELECT * from users WHERE id = 2 OR id = 5 OR id = 7
What I would like is to avoid repeating id each time in the where clause. Is there a shortcut for this in MySQL that will allow me to mention the id only once?
Yes, the IN clause
SELECT * from users WHERE id IN(2,5,7);
if these Ids you are using in the comparison come from another table you can even do
SELECT * FROM users WHERE id in (SELECT other_id FROM other_table WHERE somecondition)
e4c5 gave you the answer you needed, but here is something else you can do with IN:
select * from users where 'steve' IN (users.fname, users.lname)
Related
I have one table with user and their posts. It looks like "user_id | post_id | post_status".
Now I have a list of userid (ex, 100 users) and I want to know how many of them has at least one post that gets deleted (ex, post_status 3).
Here is my sample search:
select count(distinct user_id)
from post_table
where user_id in ( {my set} )
and post_status=3
It runs super slow since it iterates the entire table. Is there a way to speed up the query?
Use something like
SELECT COUNT(*)
FROM
-- the list of userid as a rowset
( SELECT 123 AS user_id UNION ALL
SELECT 456 UNION ALL
-- ...
SELECT 789
) user_id_list
WHERE EXISTS ( SELECT NULL
FROM post_table
WHERE post_table.user_id = user_id_list.user_id
AND post_table.post_status = 3 )
If your MySQL version is 8.0.4 or above then you may provide the users list as CSV/JSON and parse it using JSON_TABLE (the query text will be more compact).
INDEX(post_status, user_id)
may help speed up your query, especially if very few rows have status=3.
This could also speed up Akina's solution.
I am using MySQL and am getting an error when if I try to excute a subquery... my subquery is as follows:
sponsor_id columns contains 10 record with id (auto increment).
SELECT * FROM user where id=(select id from user where sponsor_id ='10002')
Thanks in advance
use IN instead of = .
SELECT * FROM user where id IN (select id from user where sponsor_id ='10002' AND id IS NOT NULL)
The reason is that your subquery is most probably returning more than one values. It should return only one value if you are using the equals to operator.
Else use the IN clause as:
SELECT * FROM user where id IN (select id from user where sponsor_id ='10002')
Edit:
You may also use an INNER JOIN or any other JOIN for that matter that suits your purpose.
you are getting the error because '=' will operate on a single value not on the multiple values.so use 'IN' opeartor or make sure that your subquery returns only a single value while using '='.
Try this
SELECT * FROM user where id IN (select id from user where sponsor_id ='10002')
or:
SELECT * FROM user where id =(select id from user where sponsor_id ='10002' Limit 1)
Im trying to get the id ,feename from 1 table id is not in studentfeeTable where invoiceID is 5 but i Got this error. I dont know how to deal with it. please help
select id, Fee_Head_Name from admission_fees_structure Where ID NOT IN (Select * from
student_fee_detail where invoiceID=5) ;
You have used "ID NOT IN (Select * from student_fee_detail where invoiceID=5)".
You should use "ID NOT IN (Select ID from student_fee_detail where invoiceID=5)".
You should compare one column with other. not entire row.
Its better to use exist in this scenario.
select id, Fee_Head_Name
FROM admission_fees_structure outer
Where
exists
(Select 1 from Student_fee_detail inner
where inner.invoiceID=5 and inner.ID = outer.ID) ;
In your Sub query you must select Id not the '*' because you are comparing it with the ID. You cannot compare a single column with the entire row. how would MySQl know that which value in the entire row is to compare with the ID.
it must be like this
Select Id from
student_fee_detail where invoiceID=5
I have a list of ids, and I want to query a mysql table for ids not present in the table.
e.g.
list_of_ids = [1,2,4]
mysql table
id
1
3
5
6
..
Query should return [2,4] because those are the ids not in the table
since we cant view ur code i can only work on asumption
Try this anyway
SELECT id FROM list_of_ids
WHERE id NOT IN (SELECT id
FROM table)
I hope this helps
There is a horrible text-based hack:
SELECT
substr(result,2,length(result)-2) AS notmatched
FROM (
SELECT
#set:=replace(#set,concat(',',id,','),',') AS result
FROM (
select #set:=concat(',',
'1,2,4' -- your list here
,',')
) AS setinit,
tablename --Your tablename here
) AS innerview
ORDER BY LENGTH(result)
LIMIT 1;
If you represent your ids as a derived table, then you can do this directly in SQL:
select list.val
from (select 1 as val union all
select 2 union all
select 4
) list left outer join
t
on t.id = list.val
where t.id is null;
SQL doesn't really have a "list" type, so your question is ambiguous. If you mean a comma separated string, then a text hack might work. If you mean a table, then something like this might work. If you are constructing the SQL statement, I would advise you to go down this route, because it should be more efficient.
this is my query, and I already know the problem but I dont have an idea about how to solve it:
$queryString = "SELECT * FROM `users_list` "WHERE `user_id` like (SELECT `user_id` FROM `equipments_list` WHERE `equipment_mac` like '%$searchStringMacRevised%')"
So, this is the error sometimes I get:
Subquery returns more than 1 row
I see that the problem is that if the mac address is registered more than once, it will give me more than one user ID, and when we are going to select the information, I have too much user id to generate the table. Can you guys help me to see how can I solve this problem?
Since you are just comparing to the user id directly, you could use an IN clause, such as
SELECT * FROM users_list
WHERE user_id IN
(SELECT user_id FROM equipments_list
WHERE equipment_mac like '%$searchStringMacRevised%')
This would allow you to potentially compare to multiple user ids.
If we want only 1 user id, then you may need to use the LIMIT type of query suggested in other answers.
It means that your inner select is returning more than one row , it should exactly return 1 row in order to match record for outer query
$queryString = "SELECT * FROM `users_list` "WHERE `user_id` like (SELECT `user_id` FROM `equipments_list` WHERE `equipment_mac` like '%$searchStringMacRevised%' LIMIT 1)"