Three rows join mysql two success third no - mysql

I have three tables People, Items and Locations. People can have only 1 Items. Locations has no relation to any of 2 tables. I want to get I record join all 3. I did 2 so far people + items but 3rd I keep getting MySQL errors. There's no JOIN ON for location. Any help?
SELECT * FROM ITEMS i
RIGHT JOIN PEOPLE p
ON (p.ITEM_ID =i.ID) where
p.ID=3
RIGHT JOIN
SELECT * FROM LOCATIONS lo where lo.ID=7

If there are no join keys in common, then you might want to do a cross join. This produces a Cartesian product, that is, every location for each row selected from People/items:
SELECT *
FROM ITEMS i RIGHT JOIN
PEOPLE p
ON (p.ITEM_ID =i.ID) cross join
location l
WHERE p.ID=3
By the way, MySQL has a very flexible (and non-standard) join syntax. You can actually leave the on clause off of a join and it will behave the same as a cross join. That is a bad habit, of course. If you want a cross join, then use cross join explicitly.

I'm assuming you want the location with the ID of 7 to appear on every row...
SELECT *
,(SELECT loc.name FROM Location loc WHERE loc.ID = 7) AS Location
FROM ITEMS i
RIGHT JOIN PEOPLE p
ON (p.ITEM_ID =i.ID)
WHERE p.ID=3
OR
SELECT *
FROM ITEMS i
RIGHT JOIN PEOPLE p
ON (p.ITEM_ID =i.ID)
CROSS JOIN (SELECT * FROM LOCATIONS lo where lo.ID=7) l
WHERE p.ID=3
OR
[several other ways to go about it]

Try something like this:
SELECT peo.id, it.id, loc.id
FROM People as peo
INNER JOIN Items as it on it.id = peo.id
INNER JOIN Locations as loc on loc.id = peo.id
WHERE peo.ID=3
Edit:
Your question was edited while I was typing this so my example doesn't match like it used to. Use ITEM_ID and ID as needed.
Although not recommended, you can also use
SELECT *
FROM People as peo
INNER JOIN Items as it on it.id = peo.id
INNER JOIN Locations as loc on loc.id = peo.id
WHERE peo.ID=3

Related

Subquery LIMIT causing issues when WHERE outside of subquery implemented

The subquery limits results perfectly when the WHERE clause at the end of the statement is not included. I understand that the subquery LIMIT happens first then the WHERE clause is fired on that result set and that this is a limitation/restriction of a subquery.
What I need is someone more experienced than me to help a brother out with retrieving the records with a LIMIT with the ability to restrict that result set by the WHERE clauses. Let me know if this was not explained well enough.
I also scoured the interwebs in search of the answer for hours with no luck. Your time is appreciated.
EDIT: added a crude example on SQLfiddle: http://sqlfiddle.com/#!9/2de563/4
SELECT *
FROM (SELECT * FROM parent_products LIMIT 10) pp
INNER JOIN products_variants pv ON pv.parent_id=pp.parent_id
INNER JOIN products p ON p.id=pv.product_id
INNER JOIN product_types pt ON pt.product_type_id=p.product_type
LEFT JOIN team_list t ON pp.team_id=t.team_id
LEFT JOIN photos ph ON ph.product_id=p.id
LEFT JOIN product_attributes pa ON pa.product_id=pv.product_id
LEFT JOIN attributes a ON a.id=pa.attribute_id
LEFT JOIN product_attribute_options po ON po.product_attribute_option_id=a.parent_id
WHERE t.team_id=100 AND p.active='y';
Explain select:
Below query will give you the expected output
SELECT * FROM (SELECT users.id,users.name FROM users
LEFT JOIN map ON users.id=map.user_id
LEFT JOIN locations ON locations.location_id=map.location_id
INNER JOIN type ON locations.type=type.id
WHERE type.id=1 limit 3) users
LEFT JOIN map ON users.id=map.user_id
LEFT JOIN locations ON locations.location_id=map.location_id
INNER JOIN type ON locations.type=type.id
where type.id=1;

Need to join MySql query to 3 tables

SELECT products.acctnum,products.subacctnum,NOW(),
items.amount,items.id,items.invoice_id,items.product_id,
items.po_id, invoices.customer_id, purchaseorders.vendor_id FROM items
INNER JOIN (products, invoices, purchaseorders)
ON (items.product_id=products.product_id AND items.invoice_id=invoices.id
AND items.po_id=purchaseorders.id)
This returns nothing... however..
SELECT products.acctnum,products.subacctnum,NOW(),
items.amount,items.id,items.invoice_id,items.product_id,
items.po_id, purchaseorders.vendor_id FROM items
INNER JOIN (products, purchaseorders)
ON (products.product_id=items.product_id AND purchaseorders.id=items.po_id)
Works...
SELECT products.acctnum,products.subacctnum,NOW(),
items.amount,items.id,items.invoice_id,items.product_id,
items.po_id, invoices.customer_id FROM items
INNER JOIN (products, invoices)
ON (products.product_id=items.product_id AND invoices.id=items.invoice_id)
Works...
Works for the rows I need in the result but when I join the 3rd table it doesn't work. LEFT JOIN displayed all the columns I needed but some rows were NULL.
I imagine the join clause you want looks more like this:
FROM items LEFT JOIN
invoices
ON invoices.id = items.invoice_id LEFT JOIN
purchaseorders
ON purchaseorders.id = items.po_id LEFT JOIN
products
ON products.product_id = items.product_id
I'm not sure which fields are not valid when you select them, but you can probably fix such issues by using coalesce() with appropriate fields from invoices and purchaseorders.

Confused about using UNION or JOIN

I have three MySQL tables: 'people', 'people2id', 'places'. The first one stores information about people, where the 'places' table stores information about their addresses. The middle one, 'people2id', interconnects this two tables (could be that one person has two or more addresses).
Now I want to go to some person's profile and see his profile, but also his associated addresses. I created this query for that:
SELECT * FROM people p
JOIN people2id e ON p.peopleId = e.peopleId
JOIN places a ON a.peopleId = e.peopleId
WHERE p.peopleId = number
This works when the person has associated address(es), otherwise, it will fail. I do not understand if I should use any kind of JOIN or use UNION for this matter.
Change the JOIN to LEFT JOIN, i.e.
SELECT * FROM people p
LEFT JOIN people2id e ON p.peopleId = e.peopleId
LEFT JOIN places a ON a.peopleId = e.peopleId
WHERE p.peopleId = number
Using LEFT JOIN will include all the records from people, whether they contain associated records or not.
UNION is used for get same kind of informations from two different queries and return them as a single result set
JOIN is used for add columns and data to rows (not as simple but you can see it like this)
You should use LEFT JOIN.
SELECT * FROM people p
JOIN people2id e ON p.peopleId = e.peopleId
LEFT JOIN places a ON a.peopleId = e.peopleId
WHERE p.peopleId = number

How to join mysql query with multiple tables

I have these tables
AssigenmentList --linksto ---School,AgeGroup
Users will have birthday attached to it
AgeGroup in turn linked to many ages like AgeGroup 3-4 is linked to one to many with 3,4 in numerical format
Now i want all the Assignment list which are linked to particular SCHOOL belong to same age as the age of Child
As a general rule:
select a.*, b.*, c.* from
A a inner join B b on a.idB = b.id
inner join C c on b.idC = c.id
You use inner join if a.idB must have match to add the row to the resultset. Left outer join if the mere presence of a.idB (left side) is enough to project the row.
The trick is to navigate from the starting table to the last joining the columns that ties them.

Getting counts of 0 from a query with a double group by

I'm trying to write a query that gets the counts for a table (call it item) categorized by two different things, call them type and code. What I'm hoping for as output is the following:
Type Code Count
1 A 3
1 B 0
1 C 10
2 A 0
2 B 13
2 C 2
And so forth.
Both type and code are found in lookup tables, and each item can have just one type but more than one code, so there's also a pivot (aka junction or join) table for the codes. I have a query that can get this result:
Type Code Count
1 A 3
1 C 10
2 B 13
2 C 2
and it looks like (with join conditions omitted):
SELECT typelookup.name, codelookup.name, COUNT(item.id)
FROM typelookup
LEFT OUTER JOIN item JOIN itemcodepivot
RIGHT OUTER JOIN codelookup
GROUP BY typelookup.name, codelookup.name
Is there any way to alter this query to get the results I'm looking for? This is in MySQL, if that matters. I'm not actually sure this is possible all in one query, but if it is I'd really like to know how. Thanks for any ideas.
A CROSS JOIN between typelookup and codelookup should solve this problem:
SELECT t.name, c.name, COUNT(item.id)
FROM typelookup t
CROSS JOIN codelookup c
LEFT JOIN itemCodePivot icp ON icp.codeId = c.codeId
LEFT JOIN item i ON i.itemId = icp.itemId AND i.TypeId = t.TypeId
GROUP BY t.name, c.name
Can you use cross join for this?
http://dev.mysql.com/doc/refman/5.0/en/join.html
SELECT typelookup.name, codelookup.name, COUNT(item.id)
FROM codelookup CROSS JOIN typelookup
LEFT OUTER JOIN item JOIN itemcodepivot
GROUP BY typelookup.name, codelookup.name
Cross join should get all types joined with all codes.. Hard to make correct SQL when you don't have all the table declarations but you can give it a try..
Note, that you should substitute your actual JOIN conditions instead of using mine.
SELECT tl.name, cl.name, IFNULL(COUNT(i.id),0)
FROM typelookup tl
LEFT JOIN item i ON tl.item_id = i.id
LEFT JOIN itemcodepivot icp ON icp.item_id = i.id
LEFT JOIN codelookup cl ON cl.id = icp.codelookup_id
GROUP BY tl.name, cl.name;
Try this and see how your counts look.
I'm not understanding your need for a RIGHT JOIN on codelookup. If this (edit) doesn't give you what you want, try adding that back in.