I have a one-to-many table relation between table1.id and table2.parent_id and want to select table1.id based on table2.status.
SELECT table1.id FROM table1
INNER JOIN table2 ON table2.parent_id = table1.id
WHERE table2.status = 1
This does pretty much what I want if there is only one relation in table2. But if there are more rows in table2 one result may have status=1 but the other have status=2.
What I want is to get a result for table1.id only if all results in table2 is of status=1.
Example;
table1
id=1, name=row1
id=2, name=row2
table2
id=1, parent_id=1, status=1
id=2, parent_id=1, status=2
id=3, parent_id=2, status=1
id=4, parent_id=2, status=1
In the example above table1.id = 1 has 2 relations with different statuses, I don't want this row. table1.id = 2 however have 2 relations with the same status=1, I want this result.
You can use EXISTS and NOT EXISTS to check for values in the other table.
This query selects all rows from Table1 that do have matching records in Table2, but none of those have a status different than 1.
SELECT *
FROM Table1 t1
WHERE -- Check for matching records in Table2.
EXISTS
(SELECT 'x' FROM Table2 t2
WHERE t2.parent_id = t1.id)
-- Skip rows that have a status different than 1.
AND NOT EXISTS
(SELECT 'x' FROM Table2 t2
WHERE t2.parent_id = t1.id
AND t2.status <> 1)
I wasn't sure what you want to have if there is not status in Table2 at all. If you want to return rows from Table1 that don't have any matching rows in Table2, you can just omit the first EXIST <subselect> part.
Related
I three tables. Table1, Table2, Table3. They all have a field called KB. Can I have one query to accomplish: Get all the information from table2, when Table1 KB = Table2 KB But only if table1 KB doesn't exist in Table3. I currently have 2 Queries. One to determine which KBs in Table1 don't exist in Table3. I then run a query against those results showing me all the records in Table2 that have matching KB. Can I do that all in 1 query, or 2 queries the best way?
You can do it with a combination of EXISTS and NOT EXISTS:
SELECT t2.*
FROM table2 AS t2
WHERE EXISTS (SELECT 1 FROM table1 AS t1 WHERE t1.KB = t2.KB)
AND NOT EXISTS (SELECT 1 FROM table3 AS t3 WHERE t3.KB = t2.KB)
or with an INNER join of table2 to table1 and a LEFT join of table2 to table1 from which you will return only the non matching rows:
SELECT t2.*
FROM (table2 AS t2 INNER JOIN Table1 ON t2.KB = Table1.KB)
LEFT JOIN table3 ON t2.KB = table3.KB
WHERE table3.KB IS NULL
This may return duplicate rows of table2 if the relationship of table2 and table1 is 1:n, so in this case you can use DISTINCT:
SELECT DISTINCT t2.*
....................
I don't know if there are any terms for these statements:
I have table1 and table2
table1
id link_id
1 1
1 2
1 3
table2
id link_url
1 www.a
2 www.b
3 www.c
And two different MYSQL Statements:
SELECT table1.id as id, table2.link_url as link_url FROM table1, table2 WHERE table1.link_id =1 and table2.id=1
SELECT table1.id as id, table2.link_url as link_url FROM table1, table2 WHERE table1.link_id=table2.id
I understand that they both return the same results.
Is there any difference in using either of them or doesn't it matter at all?
Yes there is Difference.
First Statement Returns only one row as you have set table1.link_id=1 and table2.id=1
Second Statement will Return every row which have link_id value in table1 similar to id value table2
link_id=table2.id select all data of your table where both coloumn are same .But WHERE table1.link_id =1 and table2.id=1 select data where link_id is 1 and id is 1.... both are diffrent
But USE JOIN is good option :-
SELECT table1.id as id, table2.link_url as link_url
FROM table1 join table2
on table1.link_id=table2.id
WHERE table1.link_id =1
We have 2 tables - Table1 and Table2
rows in Table1
id name
1 test
2 test2
Table2 is empty, structure:
id table1_id
query:
SELECT
t1.name as name,
count(t2.id) as count_show
FROM
Table1 as t1
LEFT JOIN
Table2 as t2 on t2.table1_id= t1.id
In result we see:
name count_show
test 0
We know that problem with count, but why? where error?
Why we get only first row and how output all rows?
Answer as requested:
You're doing a count. It is automatically grouped. So if the count of your row is only one row, that's what you get.
You have to add Group by t1.id To the end of your query.
The final query:
SELECT t1.name AS name, COUNT(t2.id) AS count_show
FROM Table1 AS t1 LEFT JOIN Table2 AS t2 ON t2.table1_id = t1.id
GROUP BY t1.id
Consider the following:
QUERY
SELECT * FROM
`table1`,`table2`
WHERE `table1`.`RemoteID` = `table2`.`ID`
AND `table2`.`UserID`=1
How can I change it from a SELECT to DELETE from table1 where these records match? It must only delete from table1, not table2
In less specific terms, I want to delete all records from table1 where they match some criteria of both tables (discretely and relatively)
You can use IN with sub query
DELETE FROM table1
WHERE `table1`.`RemoteID` IN (
SELECT ID
FROM table2
WHERE `table2`.`UserID`=1)
Try this,
Delete
from table1
where Id in
(select table1.Id
from table1 t1, table2 t2
where t1.RemoteID = t2.ID
AND table2.UserID = 1)
I'm trying to update multiple tables in a single query, but what I using does not seem to do any updating.
UPDATE table1,table2 SET table1.name='John Doe',table2.name='John Doe'
WHERE table1.id=1 and table2.id = 1;
Problem is, a row with the same id may not be present in both tables. How can I do an update in a case like this?
In this case, the id 1 is present in table1, but not in table2.
EDIT
The idea is the update data in both tables, even if an id does not exist in table1 or table2
Example:
id is present in table1 but not in table2 -> Update table1.
id is present in table2 but not table1 -> update table2.
id is present in both table1 and table2 update both.
id is not present in either tables -> do nothing
Try this,
UPDATE table1
LEFT JOIN table2
ON table1.id = table2.id
SET table1.name = 'John Doe',
table2.name = 'John Doe'
WHERE table1.id = 1