I have such query:
SELECT `type` FROM `data_user` table1 INNER JOIN
`user_type` table2 ON table1.username = 'sber'
I'm getting all rows from user_type, but I need only the type value in table user_type where id equals to the id from data_user.
How to fix this query for what I want?
SELECT `type`
FROM `data_user` table1
INNER JOIN `user_type` table2 ON (table1.username = 'sber' AND
table1.id = table2.id)
Related
I need to display records in table 1 where serialnumber is NOT IN table 2, 3, 4, and 5.
I currently figured out a way to display the records for table 1 where serialnumber is NOT IN table 2 with the following:
$query = "SELECT * FROM table1 WHERE serialnumber NOT IN
(
SELECT serialnumber
FROM table2
)";
I just need to add the other tables to this query. I tried adding AND
but it doesn't work. Please assist.
Add union ALL clause:
SELECT * FROM table1 WHERE serialnumber NOT IN
(
SELECT serialnumber FROM table2
UNION ALL
SELECT serialnumber FROM table3
UNION ALL
SELECT serialnumber FROM table4
...
)
Instead of subqueries, you can do a more efficient Left Join. we can simply do a left join from table1 to the rest of the tables (table1 being left table for all of them). Now, serialnumber values which do not exist in the other table(s), will have null value post Join. So, we can simply use Where serialnumber IS NULL condition to filter out:
SELECT t1.* FROM table1 AS t1
LEFT JOIN table2 AS t2 ON t2.serialnumber = t1.serialnumber
LEFT JOIN table2 AS t3 ON t3.serialnumber = t1.serialnumber
LEFT JOIN table2 AS t4 ON t4.serialnumber = t1.serialnumber
LEFT JOIN table2 AS t5 ON t5.serialnumber = t1.serialnumber
WHERE t2.serialnumber IS NULL AND
t3.serialnumber IS NULL AND
t4.serialnumber IS NULL AND
t5.serialnumber IS NULL
I have below query which will give me the ID which is in both the table.
SELECT DISTINCT IP.id
FROM `table2` IR LEFT JOIN
(SELECT id
FROM table1
WHERE item='xyz'
ORDER BY Id limit 728,91
)
AS IP on IP.id = IR.id
where IR.item='xyz'
AND IR.idr='2295'
Now I need the missing ID from table2 which exist in table 1.
You need an outer join that works the other way round. Also, you don't need a sub query. The following query will return those id in table1 that have the desired item value and for which there is no record in table2 that has the same id and certain item and idr values.
select distinct ip.id
from table1 ip
left join table2 ir
on ir.id = ip.id
and ir.item = 'xyz'
and ir.idr = '2295'
where ip.item = 'xyz'
and ir.id is null
order by ip.id
limit 728, 91
NB: If you want to also add to that the id values that exist in both table1 and table2 (with the given conditions) then there is no reason to (outer) join table2 at all.
I have two tables bound through an ID field:
table1: id, name, type
table2: id, id_table1, date, status
I have to collect all the records of the table1 that have a certain value of type field and that are not been referenced in table2 plus all the records of table1 referenced in table2 that have a certain status field value.
For the first part if I remember correctly I can use the LEFT JOIN command:
LEFT JOIN table1.name
LEFT JOIN table2
ON table2.id_table1 = table1.id
WHERE (table1.value = 'value1') AND (table2.id_table1 IS NULL);
but for the second part I'm getting lost...
I'm using MySQL 5.6 and I would like to define a View to handle this.
SELECT t1.*, t2.*
FROM table1 t1
LEFT JOIN table2 t2
ON table2.id_table1 = table1.id
WHERE (t1.type= 'value1' AND t2.id IS NULL)
OR (t2.status = 'certain status' )
I would think you could just change the WHERE to:
WHERE (table1.value = 'value1')
AND (table2.id_table1 IS NULL
OR
([the other table2 status criteria)
)
;
You can try this...
SELECT T1.*,T2.*
FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.Id=T2.Id_Table1
WHERE T1.Value = 'value1' AND T2.id_table1 IS NULL
UNION
SELECT T1.*,T2.*
FROM Table1 T1
INNER JOIN Table2 T2 ON T1.Id=T2.Id_Table1
WHERE T2.Status= 'Status Criteria'
I have 2 table table1 contains an userid and a postid. Table2 contains a userid and a username. I want to return all the userids with a certain postid and then use those userids to query table2 and get the usernames.
Is there a way to go about this? I have tried join statement and it doesn't seem to work
Use the IN function:
select username, userid from `table2` where userid in (select userid from `table` where postid = <condition>)
SELECT A.postid, A.userid, B.username FROM
tableA AS A JOIN tableB AS B ON A.userid=B.userid
select post id, user id from tableA username from tableB. Join these 2 different table columns when tableA's userid is equal to tableB's userid.
select t1.postid, t1.userid, t2.username
from table1 t2
left join table1 t2 on t1.userid = t2.userid
where t1.postid = <your postid>
Join the tables on the common columns
select t1.postid, t1.userid, t2.username
from table1 t1
join table2 t2 on t1.userid = t2.userid
where t1.postid = 2
I have these MySQL tables:
table1:
id | writer
1 | Bob
2 | Marley
3 | Michael
table2:
user_one | user_two
1 | 2
And this query:
SELECT table1.id FROM table1 LEFT JOIN table2 ON table1.id = table2.user_one
This query will return all rows of table1 which are 1,2,3
I want to select only rows which are not found in the left joint. So it should return only row with id 3
I want sort of the opposite of INNER JOIN which will select only the rows which are found in the join. How to get the opposite like if left join exists, ignore it and move to the next row. Hope i'm clear
You could use the following query:
SELECT table1.id
FROM table1
LEFT JOIN table2
ON table1.id IN (table2.user_one, table2.user_two)
WHERE table2.user_one IS NULL;
Although, depending on your indexes on table2 you may find that two joins performs better:
SELECT table1.id
FROM table1
LEFT JOIN table2 AS t1
ON table1.id = t1.user_one
LEFT JOIN table2 AS t2
ON table1.id = t2.user_two
WHERE t1.user_one IS NULL
AND t2.user_two IS NULL;
One of the best approach if you do not want to return any columns from table2 is to use the NOT EXISTS
SELECT table1.id
FROM table1 T1
WHERE
NOT EXISTS (SELECT *
FROM table2 T2
WHERE T1.id = T2.user_one
OR T1.id = T2.user_two)
Semantically this says what you want to query: Select every row where there is no matching record in the second table.
MySQL is optimized for EXISTS: It returns as soon as it finds the first matching record.
One more note to this:
When you check against a nullable column in the joined table, depending on your needs, you may have to use NOT EXISTS (or check against other columns when using LEFT JOIN), because MySQL won't be able to distinguish between a column which is NULL, but there is an existing record in the joined table and a column which is generated as NULL because the joined table have no matching record.
Here is a query that returns only the rows where no correspondance has been found in both columns user_one and user_two of table2:
SELECT T1.*
FROM table1 T1
LEFT OUTER JOIN table2 T2A ON T2A.user_one = T1.id
LEFT OUTER JOIN table2 T2B ON T2B.user_two = T1.id
WHERE T2A.user_one IS NULL
AND T2B.user_two IS NULL
There is one jointure for each column (user_one and user_two) and the query only returns rows that have no matching jointure.
Hope this will help you.
SELECT table1.id
FROM table1
LEFT JOIN table2 ON table1.id = table2.user_one
WHERE table2.user_one is NULL
Try following query:-
SELECT table1.id
FROM table1
where table1.id
NOT IN (SELECT user_one
FROM Table2
UNION
SELECT user_two
FROM Table2)
Hope this helps you.
Try:
SELECT A.id FROM
(
SELECT table1.id FROM table1
LEFT JOIN table2 ON table1.id = table2.user_one
WHERE table2.user_one IS NULL
) A
JOIN (
SELECT table1.id FROM table1
LEFT JOIN table2 ON table1.id = table2.user_two
WHERE table2.user_two IS NULL
) B
ON A.id = B.id
See Demo
Or you could use two LEFT JOINS with aliases like:
SELECT table1.id FROM table1
LEFT JOIN table2 A ON table1.id = A.user_one
LEFT JOIN table2 B ON table1.id = B.user_two
WHERE A.user_one IS NULL
AND B.user_two IS NULL
See 2nd Demo