Main table:
(trip)
id | name
1 | USA
2 | Europe
3 | Asia
Childrens:
(plane)
id | trip_id | name | other
1 | 1 | aaa | w
2 | 1 | bbb | e
3 | 3 | ccc | rr
(boat)
id | trip_id | name
1 | 2 | jjj
2 | 2 | kkk
3 | 3 | lll
If I want to get trips with planes, then I can:
SELECT trip.* FROM trip INNER JOIN plane ON plane.trip_id = trip.id
If I want to get trips with boats, then I can:
SELECT trip.* FROM trip INNER JOIN boat ON boat.trip_id = trip.id
But how to get in one query all trips with planes or boats (or both)?
Simply use UNION ALL:
SELECT trip.* FROM trip INNER JOIN plane ON plane.trip_id = trip.id
UNION ALL
SELECT trip.* FROM trip INNER JOIN boat ON boat.trip_id = trip.id
You can achieve by LEFT join
SELECT trip.*,ifnull(plane.Name,'NA') as planetrip, ifnull(boat.Name,'NA') boattrip FROM trip
LEFT JOIN plane ON plane.trip_id = trip.id
LEFT JOIN boat ON boat.trip_id = trip.id
WHERE (plane.Name IS NOT NULL OR boat.Name IS NOT NULL)
SQL DEMO: http://sqlfiddle.com/#!9/2ae5c/10
Try left joining the trips table to the other two tables. The trick here is that we aggregate at the end by trip and detect if, for each trip, there was a match in the plane or boat tables.
SELECT t.id, t.name,
CASE WHEN COUNT(p.trip_id) > 0 THEN 'yes' ELSE 'no' END AS has_plane,
CASE WHEN COUNT(b.trip_id) > 0 THEN 'yes' ELSE 'no' END AS has_boat
FROM trip t
LEFT JOIN plane p
ON t.id = p.trip_id
LEFT JOIN boat b
ON t.id = b.trip_id
GROUP BY t.id;
id name has_plane has_boat
1 1 USA yes no
2 2 Europe no yes
3 3 Asia yes yes
Demo
You can simply use Left Join like following
select trip.*, isnull(plane.name, '-') as PlaneName,
isnull(boat.name, '-') as BoatName from trip
left join plane ON plane.trip_id = trip.id
left JOIN boat ON boat.trip_id = trip.id
It will return following table, feel free to use any field from any of these 3 tables...
If you want all the result on several rows you can use UNION
but Your table plane and boat have not the same number of column so you should use union with explict column name eg:
SELECT trip_id, plane.name, trip.name
from plane
inner join trip on trip.id = plane.trip_id
UNION
SELECT trip_id, plane.name, trip.name
from bout
inner join trip on trip.id = boat.trip_id
or if you need all values you must use a null column where needed
SELECT trip_id, plane.name, trip.name, other
from plane
inner join trip on trip.id = plane.trip_id
UNION
SELECT trip_id,plane.name, trip.name, null
from bout
inner join trip on trip.id = boat.trip_id
Related
Lets put I have these 3 tables:
main table:
ID | OtherStuff1 | OtherStuff2 | IdProvince | IdTown
-----+-------------+-------------+------------+--------
1 | Stuff1 | Stuff2 | NULL | 1
-----+-------------+-------------+------------+--------
2 | Stuff3 | Stuff4 | 1 | NULL
province table:
ID | ProvinceName
---+--------------
1 | ProvName1
town table:
ID | TownName
---+--------------
1 | TwName1
Then I use one of these sql to print the information so it appears the name instead of the Id number.
SELECT a.Id, OtherStuff1, OtherStuff2, ProvinceName, TownName
FROM main AS a
LEFT JOIN (province AS b, town AS c)
ON (a.IdProvince = b.Id AND a.IdTown = c.Id)
WHERE a.IdProvince=1;
or
SELECT a.Id, OtherStuff1, OtherStuff2, ProvinceName, TownName
FROM main AS a
LEFT JOIN (province AS b, town AS c)
ON (a.IdProvince = b.Id AND a.IdTown = c.Id)
WHERE a.IdTown=1;
The problem comes that when it prints the result it comes like this:
ID | OtherStuff1 | OtherStuff2 | ProvinceName | TownName
-----+-------------+-------------+--------------+--------
1 | Stuff1 | Stuff2 | NULL | NULL
If one of the id's is put in NULL then even if there are other id's with numbers it puts NULL too. How do I avoid that without just delete the join of that column that is null likes this:
SELECT a.Id, OtherStuff1, OtherStuff2, ProvinceName, IdTown
FROM main AS a
LEFT JOIN (province AS b)
ON (a.IdProvince = b.Id)
WHERE a.IdProvince=1;
This would work if IdTown is NULL but I don't want this solution because there are much many rows and a lot other columns with Id's and it's impossible to go one by one checking which one is NULL and which one is not.
So I want a query like this but when there is a null it doesn't mess the other Id's putting all of them in NULL.
SELECT a.Id, OtherStuff1, OtherStuff2, ProvinceName, TownName
FROM main AS a
LEFT JOIN (province AS b, town AS c)
ON (a.IdProvince = b.Id AND a.IdTown = c.Id)
WHERE a.IdProvince=1;
Sorry the expected result for this query would be like this:
ID | OtherStuff1 | OtherStuff2 | ProvinceName | TownName
-----+-------------+-------------+--------------+--------
2 | Stuff1 | Stuff2 | ProvName1 | NULL
Any ideas?
Thanks in advance for the help!
don't mix join notations (a from clause should avoid the use of , if you're using the ANSI92 standards. If using the ANSI 89 standards, then there should be no left join syntax)
I see no reason not to do two left joins back to main table (A) one for Province and one for Town...
.
SELECT a.Id, OtherStuff1, OtherStuff2, ProvinceName, TownName
FROM main AS a
LEFT JOIN province AS b
ON a.IdProvince = b.Id
LEFT JOIN town as c
ON a.IdTown = c.Id
WHERE a.IdProvince=1;
This is because the condition in your ON clause:
a.IdProvince = b.Id AND a.IdTown = c.Id
Here you are asking to LEFT JOIN 2 tables only when both conditions are met, which in your case they don't.
I would try to keep the 2 joins separate like so:
SELECT a.Id, OtherStuff1, OtherStuff2, b.ProvinceName, c.TownName
FROM main AS a
LEFT JOIN province AS b ON (a.IdProvince = b.Id)
LEFT JOIN town AS c ON (a.IdTown = c.Id)
WHERE a.IdProvince = 1;
I've got a SELECT with multiple JOINS for a paginated Tableview. In general this is working for unfiltered results.
The query looks like this:
SELECT seltable.*,
tbl2.name AS tbl2name,
tbl3.name AS tbl3name,
tbl4.name AS tbl4name
FROM
( SELECT * FROM selecttable
WHERE value = 99
ORDER BY datetime DESC
LIMIT 50 OFFSET 0 )
AS seltable
LEFT JOIN table1 AS tbl1 ON seltable.tbl1_uid = tbl1.uid
LEFT JOIN table2 AS tbl2 ON tbl1.tbl2_uid = tbl2.uid
LEFT JOIN table3 AS tbl3 ON tbl2.tbl3_uid = tbl3.uid
LEFT JOIN table4 AS tbl4 ON tbl3.tbl4_uid = tbl4.uid;
Now I've got no clue how to accomplish filtering the results with a condition related to one of the join tables.
When I just set a:
LEFT JOIN tablex AS table ON foreign_table.tblx_uid = table.uid AND {condition}
this condition regards only to the 50 results of the nested SELECT.
Is there any way to achieve using WHERE clauses on the JOIN tables in this scenario?
For sample data see http://sqlfiddle.com/#!2/fad4d/2
Expected results:
to get x team records limited to 5 team uids, where Tournament2 is one of the related tournaments for the team.
Best regards
w1ll1
Try not controlling the pagination in that subquery, instead just use a more conventional query with a composite where clause. HOWEVER, because you are using left joins take care adding filters through the where clause that would override the outer join to produce the effect of an inner join.
SELECT seltable.*,
tbl2.name AS tbl2name,
tbl3.name AS tbl3name,
tbl4.name AS tbl4name
FROM selecttable AS seltable
LEFT JOIN table1 AS tbl1 ON seltable.tbl1_uid = tbl1.uid
LEFT JOIN table2 AS tbl2 ON tbl1.tbl2_uid = tbl2.uid
LEFT JOIN table3 AS tbl3 ON tbl2.tbl3_uid = tbl3.uid
LEFT JOIN table4 AS tbl4 ON tbl3.tbl4_uid = tbl4.uid
WHERE seltable.value = 99
...
ORDER BY seltable.datetime DESC
LIMIT 50 OFFSET 0
Alternatively use more subqueries, like this:
SELECT seltable.*,
tbl2.name AS tbl2name,
tbl3.name AS tbl3name,
tbl4.name AS tbl4name
FROM
( SELECT * FROM selecttable
WHERE value = 99
ORDER BY datetime DESC
LIMIT 50 OFFSET 0 )
AS seltable
LEFT JOIN ( SELECT uid, name
FROM table1
WHERE 1=1 -- amend to suit
) AS tbl1 ON seltable.tbl1_uid = tbl1.uid
LEFT JOIN ( SELECT uid, name
FROM table2
WHERE 1=1 -- amend to suit
) AS tbl2 ON tbl1.tbl2_uid = tbl2.uid
LEFT JOIN ( SELECT uid, name
FROM table3
WHERE 1=1 -- amend to suit
) AS tbl3 ON tbl2.tbl3_uid = tbl3.uid
LEFT JOIN ( SELECT uid, name
FROM table4
WHERE 1=1 -- amend to suit
) AS tbl4 ON tbl3.tbl4_uid = tbl4.uid;
Here is another attempt, based on your sqlfiddle it appears that INNER JOINS may be used:
SELECT theteam.*,
trnmnt.name AS tournamentname,
cat.name AS categoryname,
sport.name AS sportname
FROM (
SELECT * FROM team
ORDER BY team.name ASC )
AS theteam
INNER JOIN tournament_team AS tntm ON tntm.team_uid = theteam.uid
INNER JOIN tournament AS trnmnt ON tntm.tournament_uid = trnmnt.uid AND trnmnt.name = 'Tournament2'
INNER JOIN category AS cat ON trnmnt.category_uid = cat.uid
INNER JOIN sport ON cat.sport_uid = sport.uid
LIMIT 5 OFFSET 0
;
The result of that query is:
| UID | NAME | TOURNAMENTNAME | CATEGORYNAME | SPORTNAME |
|-----|--------|----------------|--------------|-----------|
| 2 | Team02 | Tournament2 | Germany | Soccer |
| 3 | Team03 | Tournament2 | Germany | Soccer |
| 4 | Team04 | Tournament2 | Germany | Soccer |
| 5 | Team05 | Tournament2 | Germany | Soccer |
| 6 | Team06 | Tournament2 | Germany | Soccer |
I need to show all categories, even categories with no items.
I have this query.
SELECT
i.id,
incident_active 'Approved',
incident_verified 'Verified',
category_title 'Category',
ParentCategory 'Parent Category'
FROM
incident i
INNER JOIN
incident_category ic ON i.id = ic.incident_id
RIGHT JOIN
incident_person ip ON i.id = ip.incident_id
RIGHT JOIN
(SELECT
c1.id,
c1.parent_id,
c2.category_title ParentCategory,
CONCAT_WS(' -> ', c2.category_title, c1.category_title) category_title
FROM
category c1
left outer join category c2 ON c1.parent_id = c2.id WHERE c1.parent_id != 0) AS c ON c.id = ic.category_id
WHERE incident_dateadd > DATE_SUB(NOW(), INTERVAL 1 MONTH)
which return:
and this query:
SELECT
c1.id,
c1.parent_id,
c2.category_title ParentCategory,
CONCAT_WS(' -> ', c2.category_title, c1.category_title) category_title
FROM
category c1
left outer join category c2 ON c1.parent_id = c2.id WHERE c1.parent_id != 0
which return:
I've read several times this answer but I can not see why my right join isn't working.
The first result set should have 8 more columns, the columns of categories which parent is Protesta
UPDATE
I got it working whith the following query:
SELECT * FROM (SELECT
i.id,
incident_title 'Título',
incident_description 'Descripción',
incident_date 'Fecha',
incident_active 'Aprobado',
incident_verified 'Veficado',
person_first 'Nombres',
person_last 'Apellidos',
person_email 'Email',
category_id
-- category_title 'Categoría',
-- ParentCategory 'Categoría Padre'
FROM
incident i
INNER JOIN
incident_category ic ON i.id = ic.incident_id
RIGHT JOIN
incident_person ip ON i.id = ip.incident_id
WHERE (incident_dateadd > DATE_SUB(NOW(), INTERVAL 1 MONTH) OR incident_dateadd IS NULL)) a
RIGHT JOIN
(SELECT
c1.id,
c1.parent_id,
c2.category_title ParentCategory,
CONCAT_WS(' -> ', c2.category_title, c1.category_title) category_title
FROM
category c1
left outer join category c2 ON c1.parent_id = c2.id WHERE c1.parent_id != 0) b ON a.category_id = b.id
Although I still don't understand why it was not working with the first version, in my mind both queries are equivalent.
If anyone could explain the differences...
It's the location of your final where clause.
In your fist query, you pull all of your categories and associate them with a bunch of data, getting a compilation of rows. You then use a where clause to filter out many of those rows, some of which happen to be category rows.
Let's look at a simple example.
Table A:
X | Y
-----
1 | hi
2 | bye
3 | what
Table B:
Z | X
-----
A | 1
B | 1
C | 2
Given these tables, if I say the following
SELECT * FROM `B` RIGHT JOIN `A` ON A.X = B.X
my result will be:
Z | X | Y
---------
A | 1 | hi
B | 1 | hi
C | 2 | bye
- | 3 | what
If, however, I add a where clause on the end of that so my query becomes
SELECT * FROM `B` RIGHT JOIN `A` ON A.X = B.X WHERE B.Z > 'A'
some of table A is filtered out. Now I have:
Z | X | Y
---------
B | 1 | hi
C | 2 | bye
However, if my query does the filtering before the join, like so:
SELECT * FROM
(SELECT * FROM `B` WHERE B.Z > 'A') AS B
RIGHT JOIN `A` ON A.X = B.X
my table still contains all the rows from A.
Z | X | Y
---------
B | 1 | hi
C | 2 | bye
- | 3 | what
It's just a matter of order. In your original query, you select all the rows then filter out some. In your working query, you first filter, then you get all the category rows you need.
I'm here with this (I'm sure it is) simple question I can't figure out how to solve.
I have this schema:
With this data:
My expected result is:
For "JOHN NASH":
PERSON_NAME | TOTAL_FRUIT | TOTAL COOKIE
----------------------------------------
JOHN NASH | 10 | 38
For "OSCAR WILDE":
PERSON_NAME | TOTAL_FRUIT | TOTAL COOKIE
----------------------------------------
OSCAR WILDE | 28 | 0
Thanks in advance.
SELECT name, IFNULL(f.total, 0) AS total_fruit, IFNULL(c.total, 0) AS total_cookie
FROM person AS p
LEFT JOIN (SELECT person_idperson, SUM(cost) AS total
FROM fruit
GROUP BY person_idperson) AS f
ON p.idperson = f.person_idperson
LEFT JOIN (SELECT person_idperson, SUM(cost) AS total
FROM cookie
GROUP BY person_idperson) AS c
ON p.idperson = c.person_idperson
SELECT p.name AS PERSON_NAME,
IFNULL(SUM(f.cost),0) AS TOTAL_FRUIT,
IFNULL(SUM(c.cost),0) AS TOTAL_COOKIE
FROM person AS p
LEFT JOIN fruit as f
ON p.idperson = f.person_idperson
LEFT JOIN cookie as c
ON p.idperson = c.person_idperson
GROUP BY p.idperson
I'm rather new to normalizing tables and I'm having some troubles wrapping my head around getting the correct information out of 3 tables. I made an example involving reserving books out of different libraries. I have 3 tables. Books, locations, reservations (listed below):
//SQL query:
$sql =
"SELECT * FROM books
JOIN (location LEFT JOIN reservations ON location.locID = reservations.locID)
ON books.bookID = location.bookID
WHERE location.locID=2
";
and the output I was hoping to achieve if I were to list the books in Campus B:
title |locName |status
Book 1|Campus B|1
Book 2|Campus B|0
Book 3|Campus B|0
Book 4|Campus B|0
Book 5|Campus B|1
For some reason I'm definitely not getting the output I think I should, and I was curious if anyone has some suggestions. I'm sure once I see whats going on I'll understand what I did incorrectly.
table: books
bookID|title
1 | Book 1
2 | Book 2
3 | Book 3
4 | Book 4
5 | Book 5
table: location
locID|locName
1 | campus A
2 | campus B
3 | campus C
table: reservations
bookID|locID|status
1 | 1 | 1
3 | 1 | 1
4 | 1 | 1
1 | 2 | 1
5 | 2 | 1
4 | 3 | 1
5 | 3 | 1
I think this is more in line with what you're looking for:
SELECT *
FROM books b
LEFT JOIN reservations r ON b.bookID = r.bookID
JOIN location l on r.locID = l.locID
WHERE l.locID = 2
This will return a list of the books reserved at the location with locID=2.
In this case I have a LEFT JOIN to preserve your original query, however given your WHERE clause any records with NULL in the location.locID field will not be selected.
Therefore, I could re-write your query with all INNER joins, like so:
SELECT *
FROM books b
JOIN reservations r ON b.bookID = r.bookID
JOIN location l on r.locID = l.locID
WHERE l.locID = 2
Other queries that you might find interesting:
Get ALL books, regardless of whether or not they are reserved anywhere:
SELECT *
FROM books b
LEFT JOIN reservations r ON b.bookID = r.bookID
JOIN location l on r.locID = l.locID
Get ALL locations, regardless of whether or not there are books reserved there:
SELECT *
FROM books b
JOIN reservations r ON b.bookID = r.bookID
RIGHT JOIN location l on r.locID = l.locID
Get ALL books and ALL locations:
SELECT *
FROM books b
LEFT JOIN reservations r ON b.bookID = r.bookID
RIGHT JOIN location l on r.locID = l.locID
SELECT
books.title
, location.locName
, IFNULL(reservations.status, 0) status
FROM
books
JOIN location
LEFT JOIN reservations ON (
location.locID = reservations.locID
AND books.bookID = location.bookID
)
WHERE location.locID = 2