mysql join 2 tables - show all rows from one table - mysql

I have two tables, like so:
table "a" contains:
id|name
stock1|fullname
stock2|fullname2
stock3|fullname3
table "b" contains product quantities for given stock.
id|stock_id|quantity|product_id|
1|stock1|3|13
2|stock3|4|13
3|stock1|1|5
4|stock2|2|2
Now I would need to combine those two tables, so that each product takes its stock full name from table "a", and if its quanitity is not given for stock, it would still show the row with the quanitity as 0.
So from my example, product_id 13 would show as:
stock|quanitity|product_id|stock_fullname
stock1|3|13|fullname1
stock2|0|13|fullname2
stock3|4|13|fullname3

You should be able to use a LEFT JOIN to achieve this.
SELECT a.id AS stock, COALESCE(b.quanitity,0), b.product_id, a.name AS stock_fullname
FROM a
LEFT JOIN b
ON a.id = b.stock_id
AND b.product_id = 13

It sounds like you need to use a LEFT JOIN, although the records with no quantity might show as NULL rather than zero. Something like:
SELECT a.*, b.*
FROM table_a a
LEFT JOIN table_b b ON a.stock_id = b.stock_id

try this:
SELECT stock,COALESCE(quanitity,0),product_id,stock_fullname FROM stock JOIN product

You need an outer join so that rows from the a table without a corresponding row in b are still considered. An inner join, by contrast, insists that you have a matching row. If you are pulling a value from the table where you don't have a row, you get NULL. Syntax varies between DBs and there is a distinction made depending on if it's the table on the left or right that gets the fake rows.
see other answers for syntax.

I think this query should work for your example:
SELECT a.id stock if(b.quantity IS NULL, 0, b.quantity),
b.product_id, a.name stock_fullname
FROM b
LEFT JOIN a b.stock = a.id
WHERE b.product_id = 13;

You should be able to use a LEFT JOIN to achieve this.
SELECT a.id AS stock, COALESCE(b.quanitity,0), b.product_id, a.name AS stock_fullname
FROM a
LEFT JOIN b
ON a.id = b.stock_id
AND b.product_id = 13

Related

how to merge 2 related tables even foreign key is null in MySQL? [duplicate]

I have two tables, like so:
table "a" contains:
id|name
stock1|fullname
stock2|fullname2
stock3|fullname3
table "b" contains product quantities for given stock.
id|stock_id|quantity|product_id|
1|stock1|3|13
2|stock3|4|13
3|stock1|1|5
4|stock2|2|2
Now I would need to combine those two tables, so that each product takes its stock full name from table "a", and if its quanitity is not given for stock, it would still show the row with the quanitity as 0.
So from my example, product_id 13 would show as:
stock|quanitity|product_id|stock_fullname
stock1|3|13|fullname1
stock2|0|13|fullname2
stock3|4|13|fullname3
You should be able to use a LEFT JOIN to achieve this.
SELECT a.id AS stock, COALESCE(b.quanitity,0), b.product_id, a.name AS stock_fullname
FROM a
LEFT JOIN b
ON a.id = b.stock_id
AND b.product_id = 13
It sounds like you need to use a LEFT JOIN, although the records with no quantity might show as NULL rather than zero. Something like:
SELECT a.*, b.*
FROM table_a a
LEFT JOIN table_b b ON a.stock_id = b.stock_id
try this:
SELECT stock,COALESCE(quanitity,0),product_id,stock_fullname FROM stock JOIN product
You need an outer join so that rows from the a table without a corresponding row in b are still considered. An inner join, by contrast, insists that you have a matching row. If you are pulling a value from the table where you don't have a row, you get NULL. Syntax varies between DBs and there is a distinction made depending on if it's the table on the left or right that gets the fake rows.
see other answers for syntax.
I think this query should work for your example:
SELECT a.id stock if(b.quantity IS NULL, 0, b.quantity),
b.product_id, a.name stock_fullname
FROM b
LEFT JOIN a b.stock = a.id
WHERE b.product_id = 13;
You should be able to use a LEFT JOIN to achieve this.
SELECT a.id AS stock, COALESCE(b.quanitity,0), b.product_id, a.name AS stock_fullname
FROM a
LEFT JOIN b
ON a.id = b.stock_id
AND b.product_id = 13

Check to see if the primary key exists in other multiple tables in mysql [duplicate]

I have two tables, like so:
table "a" contains:
id|name
stock1|fullname
stock2|fullname2
stock3|fullname3
table "b" contains product quantities for given stock.
id|stock_id|quantity|product_id|
1|stock1|3|13
2|stock3|4|13
3|stock1|1|5
4|stock2|2|2
Now I would need to combine those two tables, so that each product takes its stock full name from table "a", and if its quanitity is not given for stock, it would still show the row with the quanitity as 0.
So from my example, product_id 13 would show as:
stock|quanitity|product_id|stock_fullname
stock1|3|13|fullname1
stock2|0|13|fullname2
stock3|4|13|fullname3
You should be able to use a LEFT JOIN to achieve this.
SELECT a.id AS stock, COALESCE(b.quanitity,0), b.product_id, a.name AS stock_fullname
FROM a
LEFT JOIN b
ON a.id = b.stock_id
AND b.product_id = 13
It sounds like you need to use a LEFT JOIN, although the records with no quantity might show as NULL rather than zero. Something like:
SELECT a.*, b.*
FROM table_a a
LEFT JOIN table_b b ON a.stock_id = b.stock_id
try this:
SELECT stock,COALESCE(quanitity,0),product_id,stock_fullname FROM stock JOIN product
You need an outer join so that rows from the a table without a corresponding row in b are still considered. An inner join, by contrast, insists that you have a matching row. If you are pulling a value from the table where you don't have a row, you get NULL. Syntax varies between DBs and there is a distinction made depending on if it's the table on the left or right that gets the fake rows.
see other answers for syntax.
I think this query should work for your example:
SELECT a.id stock if(b.quantity IS NULL, 0, b.quantity),
b.product_id, a.name stock_fullname
FROM b
LEFT JOIN a b.stock = a.id
WHERE b.product_id = 13;
You should be able to use a LEFT JOIN to achieve this.
SELECT a.id AS stock, COALESCE(b.quanitity,0), b.product_id, a.name AS stock_fullname
FROM a
LEFT JOIN b
ON a.id = b.stock_id
AND b.product_id = 13

Joining multiple MySQL Tables with one relations ID

I have 4 MySQL tables on identity column is in common between all these tables,
Tables in Sequence:
1- Items.
2- Sales.
3- Puchases.
4- Returned.
ItemID appears in all of these tables, WHEN i use LEFT JOIN i get duplicates like:
select
a.ItemID AS ItemID,
a.Item_title AS ItemTitle,
SUM(b.qty) AS SoldQty,
SUM(c.qty) AS PurQty,
SUM(d.qty) AS RetQty
from items a
left join sales b on a.ItemID = b.items_ItemID
left join purchases c on a.ItemID = c.items_itemID
left join returned d on a.ItemID = d.items_ItemID
group by a.ItemID
That query was one of the many tries that i've tried :D the result i get is always unique for sales but duplicates for other tables ..
Thanks for the answer.
If I understand what you're trying to do correctly, you want the total number of sales, purchases, returned etc per item id from a. If that's the case, try thinking about an easier piece of the problem first: how do I get the sum of each sales quantity, grouped by item id?
To do that you'd do something like:
select b.items_ItemID, sum(b.qty) as total_sales_qty
from sales b group by b.items_ItemID
You could do the same thing for tables c and d.
Once you've got those, you can join them all together like this:
select a.ItemID, bb.total_sales_qty, cc.total_purchases_qty, dd.total_returned_qty
from items a
left join (
select b.items_ItemID, sum(b.qty) as total_sales_qty
from sales b group by b.items_ItemID) bb
on a.ItemID = bb.items_ItemID
left join (
select c... etc) cc
on a.ItemID = cc.items_ItemID
... etc
I don't really use MySQL, but in SQL Server, you could use the distinct directive, so duplicate rows appear as just a single row. All the returned columns must be identical, mind you.
Select Distinct a.ItemID AS ItemID,... From....
Hope this helps

Search data from two table with joins how to handler null values

I have write a search query that will joining two different table. i have putted left join on both. Now first table contains 60records while based on that second table has only 30. Now i wanted if i search query should return all 60records. right now it is returning 30.
query same.
select A.,B. from A left join B on A.Id=B.AId where
A.name=IfNull('tst',A.name) AND B.class=IFNull('c',B.class).
Please guide me, Thanks.
It's wise to remember that JOIN operations (all kinds of JOIN operations, LEFT, RIGHT, INNER, OUTER) have the purpose of creating a new, virtual, table that is assembled from the tables joined together.
What is this JOINed virtual table supposed to have in it? In your case, what is the meaning of your column A.ID, and your column B.AID?
Are there rows in your A table with A.ID column values which occur no times in B.AID?
Are there rows in your B table with B.AID column values which occur no times in A.ID?
If the answer to question 1 is Yes and question 2 is No, then a LEFT JOIN will give you want you want. But simplify your query. Try this.
SELECT A.*, B.*
FROM A
LEFT JOIN B ON A.ID = B.AID
If you happen to want only the rows from A where there is no corresponding row from B, try this.
SELECT A.*
FROM A
LEFT JOIN B ON A.ID = B.AID
WHERE B.AID IS NULL
If the answer to both questions is Yes, then you may want this:
SELECT A.*, B.*
FROM A
OUTER JOIN B ON A.ID = B.AID
But you should think this through very carefully.
Try this Logic i hope it will work for you.....
select A.*,B.* from A left join B on A.Id=B.AId where B.Id != ''

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.