Lets say I have two tables:
Table1
Id Name
1 Joe
2 Greg
3 Susan
4 Max
Table2
Uid comment
2 Good customer
4 Great guy
What I want to do is list all elements of Table 1, and if Table1.Id = Table2.Uid I want to select this comment. If comment does not exist give blank field.
Result should be:
1 Joe
2 Greg Good customer
3 Susan
4 Max Great Guy
I can't figure out how to do it, if I write:
select
table1.Id,
table1.Name,
table2.comment
where
table1.id=table2.Uid
It gives me only users 2 and 4.
Try to use left join it shows you all data from table1
select t1.Id, t1.Name, t2.comment
from table1 t1
left join table2 t2 on t1.id=t2.Uid
NOTE:
Good practice is to use aliases as above. Code is more readable.
select
table1.Id,
table1.Name,
table2.comment
from table1 left outer join table2 on table1.id=table2.Uid
It is a classical JOIN operation:
SELECT
t1.id, t1.name, t2.comment
FROM
Table1 AS t1
LEFT JOIN
Table2 AS t2 ON t1.id = t2.uid
Related
I have TWO tables:
t1 contain ID_car (unique), name
t2 contain ID_car(from t1), status (many status records on same ID_car)
And i need the following result:
All ID_car FROM t1 WITHOUT status = something
I already try it INNER, LEFT, RIGHT JOIN and didn't work.
How can i do that?
Many thanks for help!
More details:
t1
------------
ID_car name
------------------
1 Toyota
2 Honda
3 Mazda
4 Ford
t2
-----------------
ID_car status
1 ok
1 not_ok
2 ok
4 not_ok
ID_car 3 din not have any records in t2 but i want to display result
And i need the following result (all car from t1 without car status not_ok):
the expected result
-----------------
ID_car status
2 ok
3
Update 2
Finally solved! Thanks for help!
That's works for me:
SELECT * FROM t1
WHERE t1.ID_auto NOT IN
(SELECT DISTINCT t1.ID_auto FROM t1, t2 WHERE t1.ID_auto = t2.ID_auto AND t2.category='not_ok')
-- updated per comment below. This update will return all records from T2 (even if not existing in T1) but only if T2.Status != something
-- this should do what you want. It will give you all the records in T1, and any data in T2 (but not required to be in that table) where your T1.status is not something
SELECT *
FROM t1
LEFT JOIN t2 ON T1.ID = T2.ID
-- did the logic on the JOIN here instead of in where clause, because doing in where clause would force records to appear in t2 table (basically converting it to an inner join) doing it in the join itself does not cause this to happen
AND T2.Status != 'something'
SELECT ID_car
FROM t1
LEFT JOIN t2
ON t1.ID_car=t2.ID_car
WHERE t2.status=something
SELECT ID_car
FROM t1
LEFT JOIN
t2 on t1.ID_car=t2.ID_car
WHERE NOT t2.status='something'
Created from top of my head, hopefully it works!
Maybe if you could post the queries you wrote, maybe they could show us why the joins don't work because it should.
I do not know if I understand correctly but try and let me know:
SELECT DISTINCT t1.ID_car FROM t2 INNER JOIN t1 ON t2.ID_car = t1.ID_car WHERE t2.status != 'something'
So I have this mysql statement and I want to get the value of id just once. This is what i've tried with but it still writes the ids multiple times.
SELECT table1.id, table3.location FROM table1, table3
UNION
SELECT table2.id, table3.location FROM table2, table3
GROUP BY id
It gives example:
1 - swe
1 - swe
2 - uk
2 - uk
etc
I want:
1 - swe
2 - uk
Any suggestions anyone? Greatful for any help! Thanks
Just leave the group by:
SELECT table1.id, table3.location FROM table1, table3
UNION
SELECT table2.id, table3.location FROM table2, table3
As pointed out by W3C, UNION already shows only distinctive values by default:
http://www.w3schools.com/sql/sql_union.asp
You no need to add group by clause cause UNION will already distinct your 2 result sets.
SELECT table1.id, table3.location FROM table1, table3
UNION
SELECT table2.id, table3.location FROM table2, table3
SQL Fiddle Demo
SELECT t1.id, t3.location FROM table1 t1
inner join table3 t3 on (t1.id = t3.id)
UNION
SELECT t2.id, t3.location FROM table2 t2
inner join table3 t3 on(t2.id = t3.id)
Group by 1;
I have 2 tables in a MySQL DB:
Table 1 : id, galleryname
Table 2 : galleryid, <many other fields...>
Using PHP, I need to select all rows in Table 1 based on its ID where that id (galleryid) does not appear in Table 2.
Example:
Table 1
1, flowers
2, water
3, mountains
4, winter
Table 2
3, ...
would return these rows from Table 1
1, flowers
2, water
4, winter
I'm not exactly sure how to go about this. I am pretty good at the basics of MySQL but I suspect this is a JOIN or a UNION that is out of my league.
Any help is appreciated.
Try this:
SELECT * FROM table1
WHERE id NOT IN
(SELECT galleryid FROM table2)
or
SELECT * FROM table1
LEFT JOIN table2
ON table1.id = table2.galleryid
WHERE table2.galleryid IS NULL
Left join brings all the t1 records, then filter out those that have t2.galleryid NULL (no records in t2)
SELECT id, galleryname
FROM table1 AS t1
LEFT OUTER JOIN table2 AS t2 ON t1.id = t2.galleryid
WHERE t2.galleryid IS NULL
SELECT * FROM table1
LEFT JOIN table2
ON table1.id = table2.galleryid
WHERE table2.galleryid IS NULL
Someone posted an answer (then deleted it) that gave me ONLY the record that was in both. All others here seemed to give errors but using that original post I made one change and it worked.
Original:
SELECT * FROM table1 INNER JOIN table2 ON galleries.id = images.galleryid
(this gave me just the one that was in both)
Adding the !:
SELECT * FROM table1 INNER JOIN table2 ON galleries.id != images.galleryid
(this gave me what I needed)
Below is my Table Structure
Table 1
id
name
Table 2
id
table1_id
I want the rows from table 1 which have no reference value in table 2.
Example data:
Table 1
id name
1 demo
2 demo2
3 demo3
Table 2
id table1_id
1 1
2 1
3 1
4 3
5 3
So there is no value in table 2 with table1_id 2. I want id 2 from Table 1.
Below id the query i have tried:
SELECT l.id FROM Table1 l WHERE l.id NOT IN (SELECT DISTINCT(r.id) FROM table2 r);
This is returning a proper result but its taking more than 2 minutes to process.
In table 1 i have 4000 rows and in table 2 I have 40000 rows.
Any optimisation to above query or any alternative solution?
SELECT * FROM table1 LEFT JOIN table2
ON table1.id=table2.table1_id
WHERE table2.table1_id IS NULL
Have an index for Table1.id and Table2.table1_id, then try the following query:
SELECT Table1.id FROM Table1
WHERE Table1.id NOT IN (SELECT Table2.id FROM Table2 group by Table2.table1_id);
What you are trying to acheive is to find orphan records right?
A join that shows all the records from the first(the left) table and the matching values form the other or nulls for no matches is called a left join. I think a left join will do the same job but it is not going to be any faster. Joins are in general slower.
I found a place where it is all well explained - http://explainextended.com/2009/09/15/not-in-vs-not-exists-vs-left-join-is-null-sql-server/
It does not hurt to try with a join though, and tell us were your results the same as expected.
select t1.id from table1 as t1
left outer join table2 as t2
on t2.table1_id = t1.id
where t2.id is null;
or
select t1.id from table1 as t1
where not exists (select 1
from table2 as t2 where t2.table1_id = t1.id);
I need to make a MySQL query to show data from 3 diferents tables.
This is the table 1:
TABLE1
id
reference
name
email
This is the table 2:
TABLE2:
id
phone
This is the table 3:
TABLE3:
id
phone
I need to show all data from table1, and also the phone from table2 or table3, only if the id in table2 or table3 is the same number that is in the reference field in table1.
Any advice? Thank you!
You can try something like
SELECT t1.*
COALESCE(t2.phone,t3.phone) phone
FROM Table1 t1 LEFT JOIN
Table2 t2 ON t1.reference = t2.id LEFT JOIN
Table3 t3 ON t1.reference = t3.id
Have a look at COALESCE(value,...) and maybe SQL SERVER – Introduction to JOINs – Basic of JOINs
Yes, I have an advice, modify your structure. There's no point in having different tables to hold different phone numbers.
Here's something you can do:
table1( -- you should give it a better name
id,
-- reference, -- not needed now...
name,
email
);
phone_numbers(
id,
table1_id,
phone
);
Now you can do something like:
SELECT table1.*, GROUP_CONCAT(phone)
FROM table1
LEFT JOIN phone_numbers ON table1.id = table1_id
GROUP BY table1.id, name, email -- , whatever fields you have more on table1
You asked for a phone from table2 or from table3.
Because these 2 tables have common columns, we can simplify this whole thing and think about these 2 tables as a single one, by using an UNION clause:
select table1.*, v.phone
from table1
inner join (select * from table2
union
select * from table3) v on v.id = table1.reference
EDIT: corrected table names in the union
SELECT t1.*, t2.*, t3.*
FROM table1 t1 JOIN table2 t2
ON t1.reference = t2.ID
JOIN table3 t3
ON t1.reference = t3.ID
I don't know if you can do CASE statement in select in mysql, but you can try a CASE statement as a column and join. Here is some sudo code.
SELECT t1.*, CASE t2.phone IS NOT t3.phone THEN t3.phone ELSE t2.phone END CASE as PhoneNumber
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.reference = t2.id
LEFT JOIN Table3 t3 ON t1.reference = t3.id