MySQL Subquery in SELECT statement - mysql

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)

Related

Having multiple statements on where clause with same column name

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)

How to use the result of a query as the input for another query?

I want to use the result from a select query as the input value for another select query.
Here's an example:
SET #name = 'Tony';
select * from People where name=#name;
Returns a table of 2 columns
name surname
Tony Danza
Tony Bennett
I then want to use the surname of the first record to look up a subscription.
select * from Subscription where user='Danza';
Is it possible to use a placeholder / formula instead of typing in 'Danza' manually? Something like:
select * from Subscription where user=(result from first query);
I have 8 select queries that depend on the result of the previous select queries so I'm not sure if joins are the way to go. i.e. name > surname > subscription > readerID > deviceId > etc
I'm trying to locate records and then delete them. Here are the actual queries that I'm trying to use
SET #readerId=-7256784839031027017; // set manually
select * from Reader where readerId=#readerId;
SET #readersId=788216; // use the previous query's readerId column
select * from Reader_Device where READERS_ID=#readersId;
SET #deviceId=786527; // use the previous query's DEVICES_ID column
select * from Device where id=#deviceId;
SET #subscriptionValue='B1AA9B9FFBAE46918C079CAEC06EDC3B'; // use the previous query's deviceId column
select * from Subscription_attributes where KEY0='deviceId' and value=#subscriptionValue;
SET #subscriptionId=786618; // use the previous query's SUBSCRIPTION_ID column
delete from Installation where DEVICE_REF=#deviceId;
delete from Reader_Device where DEVICES_ID=#deviceId;
delete from Device where id=#deviceId;
delete from Subscription_attributes where KEY0='deviceId' and value=#subscriptionValue;
delete from Subscription_attributes where SUBSCRIPTION_ID=#subscriptionId;
delete from Subscription where id=#subscriptionId;
delete from Reader where readerId=#readerId;
by join :
select s.* from subscription s join people p on s.user = p.surname
where p.name = #name /* modify as per your requirement */
by in clause :
select * from subscription
where user in(select surname from people where name = #name)
I hope this will help.

Getting error Operand should countain 1 Column(s);

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

About a query and subquery

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)"

Combine Update and Select Query

I got two MySQL working fine and i'm trying to find a way to combine them into one single query.
First, it selects ID of an employee.
SELECT 'ID' FROM `employee` ORDER BY ID DESC LIMIT 1;
Let's say it returns ID 100;
Then update data of employees whose ID is 100
UPDATE 'LOG' SET `TIME_EXIT`='2013/02/22' WHERE `ID`='100';
Can i do it all in a single query?
Just add them together:
UPDATE LOG SET TIME_EXIT = '2013/02/22'
WHERE ID = (
SELECT ID
FROM employee
ORDER BY ID DESC
LIMIT
);
But based on that code currently it'll only ever update the last employee, you will need to select the correct employee by using some other identifier to ensure you have the correct one.
UPDATE LOG SET TIME_EXIT = '2013/02/22'
WHERE ID = (
SELECT ID
FROM employee
WHERE NAME = 'JOHN SMITH'
ORDER BY ID DESC
LIMIT 1
);
It's now a few months old, but maybe helps you or others finding this via google…
If you want to UPDATE a field in the same selected table use this:
UPDATE LOG SET
TIME_EXIT = '2013/02/22'
WHERE ID = (
SELECT ID
FROM (
SELECT ID
FROM LOG
WHERE whatEverYouWantToCheck = whateverYouNeed
) AS innerResult
)
So, you SELECT id from a subselect. If you try to subselect it directly, mySQL quites with your error message You can't specify target table 'log' for update in FROM clause, but this way you hide your subsubquery in a subquery and that seems to be fine. Don't forget the AS innerResult to avoid getting the error message #1248 - Every derived table must have its own alias. Also match the subsubquery field name to the subquery field name in case you do something like SELECT COUNT(*) or SELECT CONCAT('#', ID)