I have three tables:
person_table
id| name | gender
1 | Joe | male
2 | Jane |female
3 | Janet | female
4| Jay | male
etc...
product_table
id| name
1 | magazine
2 | book
3 |paper
4 | novel
etc...
**person_product
person_id| product_id | quantity
1 | 1 | 1
1 | 3 | 3
2 | 3 | 1
4 | 4 | 2
etc...
I have tried to make a query that will return a table like this:
person_id| person_name | product_name| quantity
but i can't make it so that if lets say John has no books, it should display
(johns id) John|book|0
instead of just skipping this line.
Where did i go wrong?
here is what i managed to come up with:
SELECT p.*, f.name, l.quantity
FROM person_product AS l
INNER JOIN people_table AS p ON l.person_id=p.id
INNER JOIN product_table AS f ON l.product_id=f.id
ORDER BY id`
It seems that you're generating a report of all people, against all products with the relevant quantity; on a large data set this could take a while as you're not specifically joining product to person for anything other than quantity:
SELECT
p.id,
p.name,
p.gender,
f.name,
IFNULL(l.quantity,0) AS quantity
FROM person_table AS p
JOIN product_table AS f
LEFT JOIN person_product AS l
ON l.person_id = p.id
AND l.product_id = f.id
ORDER BY p.id, f.name
Which results in:
Is that more-or-less what you're after?
you need to start with people_table than using left join you need to bring other table data.
as you need 0 value if null than you can use function IFNULL
SELECT p.*, f.name, IFNULL(l.quantity,0)
FROM people_table AS p
LEFT JOIN person_product AS l ON l.person_id=p.id
LEFT JOIN product_table AS f ON l.product_id=f.id
ORDER BY p.id
if has no book shouldn't appear in the table , try this (easy to understand) :
SELECT NAME
,'0'
,'0'
FROM person_table
WHERE id NOT IN (
SELECT person_id
FROM person_product
)
UNION
SELECT person_id
,product_id
,quantity
FROM person_product;
I have three tables with same structure.
table1
id | email | count
1 | test1#abc.com | 5
2 | test2#abc.com | 5
3 | test3#abc.com | 5
table2
id | email | count
1 | test1#abc.com | 50
2 | test1#abc.com | 50
3 | test3#abc.com | 50
table3
id | email | count
1 | test1#abc.com | 40
2 | test1#abc.com | 45
3 | test1#abc.com | 50
Now what i want is for table1, for first record "test1#abc.com", I need sum of "count" field of next two tables. So i used below query
SELECT (IFNULL(sum(distinct(table2.count)), 0) +
IFNULL(sum(distinct(table3.count)), 0)) as total
FROM table1
LEFT JOIN table2 ON table1.email = table2.email
LEFT JOIN table3 ON table1.email = table3.email
WHERE table1.email = 'test1#abc.com'
This query gives me below record:
185
But the result should be as below:
235
This is because i have used distinct when adding field. But if i don't use distinct, it gives me 285.
Please help. What should i do?
Your issue is because, first, you're using LEFT JOIN (no sense with summation since NULL-records will provide nothing), second, that's how JOIN works. Illustrate with query:
SELECT
t1.id AS id_1,
t1.email AS email_1,
t1.count AS count_1,
t2.id AS id_2,
t2.email AS email_2,
t2.count AS count_2,
t3.id AS id_3,
t3.email AS email_3,
t3.count AS count_3
FROM
table1 AS t1
INNER JOIN table2 AS t2 ON t1.email=t2.email
INNER JOIN table3 AS t3 ON t1.email=t3.email
WHERE
t1.email='test1#abc.com'
(fiddle is here). As you can see, you'll get repeated id's from second and third tables - and - yes, that's because there are multiple rows for joining condition.
To resolve your issue you may add distinction by id into join (and later filtering that with variables or like that), but I would not recommend it. JOIN is simply not the thing for your issue. Use UNION, like:
SELECT
SUM(`count`) AS s
FROM
(
SELECT
table2.count
FROM
table2
WHERE
email='test1#abc.com'
UNION ALL
SELECT
table3.count
FROM
table3
WHERE
email='test1#abc.com'
) AS u
(see the fiddle)
i have 2 tables
1 resources
id | name | type
1 | X | form
2 | YY | post
3 | ZZ | container
2 res_res
id | parent_id | son_id
1 | 1 | 2
2 | 3 | 1
now i want to select resource.*, res_res.id of all resources related to resource id (1)
expected result
link_id | id | name | type |
1 | 2 | YY | post |
2 | 3 | ZZ | container
my query
1:
SELECT distinct r.* FROM `resources` as r
join res_res as l on (l.parent_id=r.id or l.son_id=r.id)
where l.parent_id = 2 or l.son_id = 2
this query run as expected and return result i want except that it doesnt include the id or the link (id from resource_resource table) , yet if i run this
2:
SELECT distinct l.id as link_id,r.* FROM `resources` as r
join res_res as l on (l.parent_id=r.id or l.son_id=r.id)
where l.parent_id = 2 or l.son_id = 2
this return so many duplicate rows; so what am i doing wrong ?
I thin the problem is that my join condition return both parent-id and son-id when row match, so duplicate happens, I need to put an if case in select so that I only select other field .
is there a better way to select all resources related to resource.id X and include the join id ?
i dont like using group_by cause it writes a temp. table which slow down my performance alot.
thanks
Possibly use a UNION instead:-
SELECT l.id AS link_id, l.son_id AS id, r.name, r.type
FROM `resources` AS r
INNER JOIN res_res AS l ON l.parent_id = r.id
WHERE l.son_id = 1
UNION
SELECT l.id AS link_id, l.parent_id AS id, r.name, r.type
FROM `resources` AS r
INNER JOIN res_res AS l ON l.son_id = r.id
WHERE l.parent_id = 1
Your join condition is wrong as it matches against the son element even when parent is the one you need.
SELECT l.id as link_id,r.*
FROM `resources` as r
CROSS JOIN res_res as l
WHERE (l.parent_id = 2 AND l.parent_id=r.id) OR (l.son_id = 2 AND l.son_id=r.id)
i have three tables i would like to link in this one query.
The script is an attendance register, so it records an attendance mark for each meeting, per user.
The three tables used:
"team":
id | fullname | position | class | hidden
1 | Team | -- | black | 1
2 | Dan S | Team Manager | green | 0
3 | Harry P | Graphic Engineer | blue | 0
"register":
id | mid | uid | mark
1 | 1 | 2 | /
2 | 1 | 3 | I
3 | 2 | 1 | /
4 | 2 | 3 | /
"meetings":
id | maintask | starttime | endtime
1 | Organise Year Ahead | 1330007400 | 1330012800
2 | Gather Ideas | 1330612200 | 1330617600
3 | TODO | 1331217000 | 1331222400
There is a sample of the data. What i want to do is:
Select all the results from the register, group them by the user, and order them by the meeting start time. But, if there is not a mark in the register table, i want it to display "-" (can be done via php if needed) So an expected result like so:
fullname | mark | mid
Dan S | / | 1
Dan S | / | 2
Dan S | - | 3
Harry P | I | 1
Harry P | / | 2
Harry P | - | 3
My SQL Query is this at the moment:
SELECT u.fullname,u.id,r.mark,r.mid FROM team u FULL JOIN register r ON r.uid=u.id LEFT JOIN meetings m ON
r.mid=m.id GROUP BY u.id ORDER BY m.starttime ASC
And i get an error back from MySQL:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'FULL JOIN register r ON r.uid=u.id LEFT JOIN meetings m
ON r.mid=m.`id' at line 1
But, i cant see an issue with it :S
Please could someone help out, point me in the right direction or give me a possible solution to this. Much Appreciated
Dan
Answer:
Query that worked:
SELECT
u.fullname, u.id as uid,
if(r.uid = u.id, r.mark, '-') as mark,
if(r.uid = u.id, r.mid, '-') as mid,
r.mid, m.starttime
FROM
team u
CROSS JOIN
register r ON u.id = r.uid
LEFT OUTER JOIN
meetings m ON r.mid = m.id
WHERE
u.hidden = 0
GROUP BY
u.id, r.mid
ORDER BY
m.starttime, u.id ASC
Full outer join is not supported by MySQL. At least to version 5.6, you can check MySQL Join doc. A cross join may be a workaround:
EDITED
SELECT
UxM.fullname,
r.mark,
UxM.mid,
UxM.starttime
FROM
( select u.id as uid, m.id as mid, u.fullname, m.starttime
from
team u
CROSS JOIN
meetings ) UxM
left join
register r
on UxM.uid = r.uid and UxM.mid = r.mid
ORDER BY
UxM.starttime ASC
Let me know if this solve your issue.
A simplification:
SELECT
u.fullname,
u.id AS uid,
COALESCE(r.mark, '-') AS mark,
COALESCE(r.mid, '-') AS mid,
m.id,
m.starttime
FROM
team u
CROSS JOIN
meetings m
LEFT JOIN
register r
ON r.mid = m.id
AND r.id = u.uid
WHERE
u.hidden = 0
GROUP BY
m.id, u.id
ORDER BY
m.starttime, u.id
I have this query:
SELECT a.id as alert_id,a.user_id,a.date,a.msg_title,a.message,a.alert_type,a.school_or_contact_id,
u.id as user_id, u.full_name,
c.id as contact_id, concat(c.f_name,' ',c.l_name) as contact_name
FROM alerts a
LEFT OUTER JOIN users u ON a.user_id = u.id
LEFT OUTER JOIN contacts c ON a.school_or_contact_id = c.id
LEFT OUTER JOIN schools s ON a.school_or_contact_id = s.school_id
ORDER BY a.date
This works, but I need it to do one more thing, and I can't seem to figure it out. I need to select some data from the "schools" table IF data in alerts.alert_type (alerts table) == "claim".
If "claim" is not found in alerts.alerts_table, then it needs to do nothing different than the query above. alerts.alert_table
This is what I've tried, but it doesn't seem to work:
SELECT a.id as alert_id,a.user_id,a.date,a.msg_title,a.message,a.alert_type,a.school_or_contact_id,
u.id as user_id, u.full_name,
c.id as contact_id, concat(c.f_name,' ',c.l_name) as contact_name,
IF(a.alert_type = 'claim', select s.* from schools where school_id = a.school_or_contact_id)
FROM alerts a
LEFT OUTER JOIN users u ON a.user_id = u.id
LEFT OUTER JOIN contacts c ON a.school_or_contact_id = c.id
LEFT OUTER JOIN schools s ON a.school_or_contact_id = s.school_id
ORDER BY a.date
EDIT
For clarification, I'm building a tool that has front page "update" kind of like Facebook. Depending on what the users are doing, the "alerts" will say different things.
The schools table has 3,000 rows and will only apply to the alerts table when the row alerts_type.alerts == "claim". Otherwise, it won't matter what what's in the schools table. If alert_type.alerts != "claim", the "contacts" table will be where the rest of the data comes from.
I wanted to have cleaner data when doing the query (ie -- not "school" table data when alerts_type.alerts != "claim") but I can easily do this in PHP. I just didn't want to pull data that I wouldn't use.
Thank you everyone for all the help and advice!
2nd edit
I will change the table schema. Right now, it looks like this:
mysql> desc alerts;
+----------------------+--------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+--------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(12) | YES | | NULL | |
| date | timestamp | NO | | CURRENT_TIMESTAMP | |
| msg_title | varchar(100) | YES | | NULL | |
| message | longtext | YES | | NULL | |
| alert_type | varchar(255) | YES | | NULL | |
| school_or_contact_id | int(12) | YES | | NULL | |
+----------------------+--------------+------+-----+-------------------+----------------+
7 rows in set (0.00 sec)
I will edit the alerts table to this (below), then JOIN alerts.school_id = schools.school_id. This should fix the problem.
+----------------------+--------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+--------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(12) | YES | | NULL | |
| date | timestamp | NO | | CURRENT_TIMESTAMP | |
| msg_title | varchar(100) | YES | | NULL | |
| message | longtext | YES | | NULL | |
| alert_type | varchar(255) | YES | | NULL | |
| school_id | int(12) | YES | | NULL | |
| contact_id | int(12) | YES | | NULL | |
+----------------------+--------------+------+-----+-------------------+----------------+
You can't really do an optional JOIN like you're trying in the above SQL.
You'll need an IF clause for each column, i.e IF (a.alert_type = 'claim', s.col, NULL)
As you've already joined on the schools table, there shouldn't be any difference in performance, and fetching data all in one query will be better than running multiple queries.
An example:
SELECT a.id as alert_id,a.user_id,a.date,a.msg_title,a.message,a.alert_type,a.school_or_contact_id,
u.id as user_id, u.full_name,
c.id as contact_id, concat(c.f_name,' ',c.l_name) as contact_name,
IF (a.alert_type = 'claim', s.col1, NULL) AS col1,
IF (a.alert_type = 'claim', s.col2, NULL) AS col2
FROM alerts a
LEFT OUTER JOIN users u ON a.user_id = u.id
LEFT OUTER JOIN contacts c ON a.school_or_contact_id = c.id
LEFT OUTER JOIN schools s ON a.school_or_contact_id = s.school_id
ORDER BY a.date
If it happens that you have a lot of fields in the schools table you might as well just fetch s.*, avoid the IF parts, and simply skip over those values in your PHP script.
Probably the best way would be to check the alert_type using PHP and run a second query if needed. You could then merge the two results together.
You might try this though:
SELECT a.id as alert_id,a.user_id,a.date,a.msg_title,a.message,a.alert_type,a.school_or_contact_id,
u.id as user_id, u.full_name,
c.id as contact_id, concat(c.f_name,' ',c.l_name) as contact_name, s.*
FROM alerts a
LEFT OUTER JOIN users u ON a.user_id = u.id
LEFT OUTER JOIN contacts c ON a.school_or_contact_id = c.id
LEFT OUTER JOIN schools s ON a.school_or_contact_id = s.school_id AND a.alert_type = 'claim'
ORDER BY a.date
You can't embed queries into IF() calls. Any reason you can't just do the sub-query unconditionally and then filter the value in your client app? Regardless of this, you cannot have a subquery return multiple fields as you are when the subquery is substituting for a field. So even if the IF() call were possible, the sub-queries have to return a single field/row.
Let me introduce you to UNION SELECT.
Note this will be a long query, and depends on the exact structure of schools; the below assumes two columes xs.foo and xs.bar:
SELECT * FROM
(SELECT a.id as alert_id,a.user_id,a.date,a.msg_title,a.message,
a.alert_type,a.school_or_contact_id,
u.id as user_id, u.full_name,
c.id as contact_id, concat(c.f_name,' ',c.l_name) as contact_name,NULL,NULL
FROM alerts a
LEFT OUTER JOIN users u ON a.user_id = u.id
LEFT OUTER JOIN contacts c ON a.school_or_contact_id = c.id
WHERE xa.alert_type!='claim'
UNION SELECT xa.id as alert_id,xa.user_id,xa.date,xa.msg_title,xa.message,
xa.alert_type,xa.school_or_contact_id,
xu.id as user_id, xu.full_name,
xc.id as contact_id, concat(xc.f_name,' ',xc.l_name) as contact_name,xs.foo,xs.bar
FROM alerts xa
LEFT OUTER JOIN users xu ON xa.user_id = xu.id
LEFT OUTER JOIN contacts xc ON xa.school_or_contact_id = xc.id
LEFT OUTER JOIN schools xs ON xa.school_or_contact_id = xs.school_id
WHERE xa.alert_type='claim')
ORDER BY date
A caveat: That this is complicated is a good sign your database is poorly designed. If you inherited this...problem, then so be it, but if you're creating new code that works this way, let me strongly recommend that you model your data so a full outer join does the right thing.
As others explained, it's notpossible to have variable number of columns in a result set.
The closest you can get to what you want may be this:
SELECT a.id as alert_id
, a.user_id
, a.date
, a.msg_title
, a.message
, a.alert_type
, a.school_or_contact_id
, u.id as user_id
, u.full_name
, c.id as contact_id
, concat(c.f_name,' ',c.l_name) as contact_name
, s.*
FROM alerts a
FROM alerts a
LEFT OUTER JOIN users u
ON a.user_id = u.id
LEFT OUTER JOIN contacts c
ON a.school_or_contact_id = c.id
LEFT OUTER JOIN schools s
ON a.school_or_contact_id = s.school_id
AND a.alert_type = 'claim'
ORDER BY a.date