How to use count() correlated sub select - sql

I have this SQL query
SELECT table1.*
FROM table1 table1
WHERE table1.table2_id IN (SELECT table2.id
FROM table2
WHERE table2.locked = 0)
I get the result and it works fine, but now I want to count how many rows exist.
I tried something like this:
SELECT table1.count(*)
FROM table1 table1
WHERE table1.table2_id IN (SELECT table2.id
FROM table2
WHERE table2.locked = 0)
But nothing worked…
How can I count the rows in this kind of query?

Try this
SELECT COUNT(*)
FROM table1 table1
WHERE table1.`table2_id` IN (SELECT table2.id FROM table2 WHERE table2.locked = 0)
Hope it will help you! ..

Related

Select from table with two conditions

I'm stuck with a query and I'd need some help.
I need to select values from a table which meets two conditions from other table, for example:
Select * from table1
where ID = (select ID from table2)
AND value = (select value from table2)
So, if I'd need only one value from the table, I could query:
Select * from table1 where ID = (id1) AND value = (value1)
The only solution that I know is using IN, but it wouldn't be the requested solution.
I need something similar to this, but counting that the data returned by table2 is not only one row, but multiple.
Could somebody give me some clue on how to find this?
Thanks.
One method uses exists:
Select *
from table1 t1
where exists (select 1
from table2 t2
where t2.id = t1.id and t2.value = t1.value
);
This is ANSI standard syntax, so it should work in any database. Some databases support this syntax:
select t1.*
from table1 t1
where (t1.id, t1.value) in (select t2.id, t2.value from table2 t2);
I would use AND and OR
select *
from table1
where (ID = (id1) AND value = (value1))
OR (ID = (id2) AND value = (value2))
Each check must be added in brackets. This allows all results matching the pairs ID and VALUE to be returned.
Select * from
table1 inner join table2 on
(table1.ID = table2.ID and
table1.value = table2.value) ;
Since as far as I can understand , you just need to select every rows from table1 where ID and value in table1 equals ID and value in table2. So you just need inner join which joins table1 and table2 checking the condition.

Select data where column from table is different from column from another table

I need to select data from a table (table1) where a specific column is different from another column from another table(table2)
Something like that:
SELECT * FROM table1 WHERE table1.column1 != table2.column2
I do the change like this :
SELECT * FROM table1
inner join table2 on table1.id = table2.id
WHERE table1.column1 != table2.column2
I have a table(table1) that I store some people.I need to select random, one by one and add to another table(table2).If a person it's added ,that person can be selected one more time until all the people are selected . This is my query :
SELECT * FROM people inner join people_generated on people.id = people_generated.id WHERE people.id != people_generated.id_people ORDER BY RAND() LIMIT 1
This return a empty result : "MySQL returned an empty result set"
The second table "people_genrated" is empty so,remain people to generate.Why result is empty?
Try Something like this,
SELECT t1.name FROM table1 t1
LEFT JOIN table2 t2 ON t2.name = t1.name
WHERE t2.name IS NULL
Try this,
SELECT * FROM table1
inner join table2 on table1.id = table2.id
WHERE table1.column1 != table2.column2
Try this one.
Select rooms.single from rooms where rooms.single<>checkin.room_no;

MS Access - WHERE IN works, but WHERE NOT IN fails

I have the following query (simplified) on MS Access:
SELECT * FROM table1 WHERE table1.ID NOT IN (SELECT DISTINCT table1id FROM table2);
My problem is it doesn't work, but these two ones work:
SELECT * FROM table1 WHERE table1.ID IN (SELECT DISTINCT table1id FROM table2);
SELECT * FROM table1 WHERE table1.ID NOT IN (2, 3);
The first one simply returns me an empty set, while I know I have records on table1 with ids ranging from 1 to 9, and only 2 and 3 are use on table 2.
Any help?
Generally, the problem with IN and NOT in has to do with NULLs in the subselect. Try this and see if it works:
SELECT *
FROM table1
WHERE table1.ID NOT IN (SELECT DISTINCT table1id FROM table2 where tableid is not null);

Subquery not in performance question

I have this slow query
select * from table1 where id NOT IN ( select id from table2 )
Would this be faster by doing something like (not sure if this is possible):
select * from table1 where id not in ( select id from table2 where id = table1.id )
Or:
select * from table1 where table1.id NOT EXIST( select id from table2 where table2.id = table1.id )
Or:
select * from table1
left join table2 on table2.id = table1.id
WHERE table2.id is null
Or do something else? Like break it up into two queries ...
The question is - are the field(s) in the comparison nullable (meaning, can the column value be NULL)?
If they're nullable...
...in MySQL the NOT IN or NOT EXISTS perform better - see this link.
If they are NOT nullable...
... LEFT JOIN / IS NULL performs better - see this link.
select table1.* from table1
LEFT JOIN table2 ON table1.id = table2.id
WHERE table2.id IS NULL
The object being to get rid of NOT IN

MySQL UPDATE query with subquery taking forever

I have a MySQL UPDATE query which takes a long time to complete. Am I missing a much simpler way to achieve the same result?
"UPDATE table2, table1
SET table2.id_occurrences = (SELECT SUM(IF(id = table2.id, 1, 0)) FROM table1)
WHERE table2.id = table1.id;"
table2 contains all possible values of id, exactly one record for each.
table1 contains some values of id, but there are multiple records of some values.
I need to update records in table2 to show the number of occurrences of the corresponding value of id in table1. The above query does the job, but it takes about 3 minutes when table1 contains 500 records, and table2 30,000 records. I have much bigger tables to process so this is too long :)
Thanks in advance.
I think your join on the update is perhaps not necessary...
UPDATE table2
SET table2.id_occurrences = (SELECT COUNT(*) FROM table1
WHERE table2.id = table1.id);
Avoid subqueries, use joins:
UPDATE table2
LEFT JOIN table1 ON (table2.id = table1.id)
SET table2.id_occurrences = COUNT(table1.id)
GROUP BY table2.id
Oh, UPDATE doesn't support GROUP BY. Try this query:
UPDATE table2
LEFT JOIN (
SELECT id, COUNT(*) AS cnt FROM table1 GROUP BY id
) AS t1
ON (table2.id = t1.id)
SET table2.id_occurrences = t1.cnt
I'd go for something like:
UPDATE table2
SET id_occurrences = (SELECT count(*) FROM table1
WHERE table1.id = table2.id)
UPDATE table2, table1
SET table2.id_occurrences = (SELECT SUM(IF(id = table2.id, 1, 0)) FROM table1)
WHERE table2.id in (select distinct table1.id from table1) AND table2.id = table1.id;