MySQL join with a "bounce" off a third table - mysql

I have 3 MySQL tables.
companies with company_id and company_name
products with product_id and company_id
names with product_id, product_name and other info about the product
I'm trying to output the product_name and the company_name in one query for a given product_id.
Basically I need information from the names and companies tables and the link between them is the products table.
How do I do a join that needs to "bounce" off a third table?
Something like this but this obviously doesn't work:
SELECT product_name, company_name
FROM names
LEFT OUTER JOIN companies ON
(names.product_id = products.product_id and products.company_id = companies.company_id)
WHERE product_id = '12345'

select n.product_name, c.company_name
from names n
left outer join products p on n.product_id = p.product_id
left outer join companies c on p.company_id = c.company_id
where n.product_id = '12345'

You nearly have it, you just need to include the third table as another join in your query:
SELECT product_name, company_name
FROM names
LEFT JOIN products ON names.product_id = products.product_id
LEFT JOIN companies ON products.company_id = companies.company_id
WHERE product_id = '12345'
Also you should note that if you are using LEFT JOIN then the company name could be NULL if the company that made the product is unknown. So you need to test for that in your code to avoid an exception. If you know that it should never be NULL, or if you want to explicilty exclude products for which you don't know the company then use an INNER JOIN instead of a LEFT JOIN in both cases.

Related

SQL -- Counting with nested subquery

I have a collection of tables in a relational database
products
categories
orders
line_items
customers
Products has a many-to-many relationship with categories (join table categories_products) and also has and belongs to many orders through line_items, which is a join table for products and orders with an id. A customer also has many orders.
I'm trying to put together some SQL that will give me this sort of response:
customer_id | customer_first_name | category_id | category_name | number_purchased
-----------------------------------
1 |Jack | 1 | Electronics | 15
2 |Jill | 1 | Electronics | 2
2 |Jill | 2 | Hiking | 3
This is the giant hunk of SQL I've been trying to use to get these values:
SELECT
DISTINCT customers.id AS customer_id,
customers.first_name AS customer_first_name,
categories.id AS category_id,
categories.name AS category_name,
(
SELECT count(li.id) FROM line_items li
INNER JOIN orders o ON li.order_id = o.id
INNER JOIN products p ON li.product_id = p.id
INNER JOIN categories_products cp ON cp.product_id = p.id
WHERE
o.customer_id = customer_id
AND o.status = 3
AND cp.category_id = category_id
) AS number_purchased
FROM orders
LEFT JOIN customers ON orders.customer_id = customers.id
LEFT JOIN line_items li ON li.order_id = orders.id
LEFT JOIN products ON products.id = li.product_id
LEFT JOIN categories_products catpr ON catpr.product_id = products.id
LEFT JOIN categories ON catpr.category_id = categories.id
Only the count itself is wrong. Instead of getting the number of line items a customer has bought in a specific category, I'm instead getting a count for all LineItems that have been part of a completed order.
How can I get the count to correctly represent the number of line_items purchased by a specific customer within a category?
NOTE: in the SQL text, o.status = 3 is using an enum to indicate that an Order is "complete."
I think your inner join with categories_products is screwing this up. You should set up a fiddle, like #Strawberry suggested, or try this:
SELECT
DISTINCT customers.id AS customer_id,
customers.first_name AS customer_first_name,
categories.id AS category_id,
categories.name AS category_name,
(
SELECT count(li.id) FROM line_items li
INNER JOIN orders o ON li.order_id = o.id
INNER JOIN products p ON li.product_id = p.id
WHERE
o.customer_id = customer_id
AND o.status = 3
) AS number_purchased
FROM orders
LEFT JOIN customers ON orders.customer_id = customers.id
LEFT JOIN line_items li ON li.order_id = orders.id
LEFT JOIN products ON products.id = li.product_id
LEFT JOIN categories_products catpr ON catpr.product_id = products.id
LEFT JOIN categories ON catpr.category_id = categories.id
If you wanted to correct your count, I would advise using a GROUP BY clause in the subquery. If you GROUP BY orders then you will only get the specific order which you retrieved when looking that the user id was correct. I would encourage you to take a look at mistakes in other part of your SQL code to clean up this hulking query. For example, make sure you want to be using distinct and that you actually want to be using left joins versus inner joins, both of which could seriously mess with the performance of your program.

Using cases to determine which table should join

I have four tables products, product_histories, vendor_invoices and invoices
This is the query I have developed
SELECT p.product_id, product_name, vendor_name FROM products AS p
INNER JOIN product_histories AS ph ON p.product_id = ph.product_id
CASE
WHEN ph.history_type = "P" THEN
LEFT JOIN vendor_invoices AS vi ON link_id = vi.vi_id
WHEN ph.history_type = "S" THEN
LEFT JOIN invoices AS i ON i.invoice_id = link_id
END
ORDER BY ph_id ASC
What I want that if ph.history_type is P then is should join vendor_invoices and if it is S then it should join invoices. But it says there is a syntax error.
Can anyone help me out with it? Or could show a better way to achieve this problem.

Find missing combination of columns from two mysql tables

I have one product table like this:
productid categoryid
and another category table like this:
categoryid parentid
would like to find rows missing from product table
select distinct c.parentid, pc.productid, from products_categories pc
join categories c on pc.categoryid = c.categoryid
where concat(pc.productid,'-',c.parentid) not in (
select distinct concat(productid,'-',categoryid) from products_categories
)
however this is extremely slow. is there a way to do this with joins instead of the not in concat line? the concat is used to account for all possible combos.
Everytime you are adding a prefix productID & - to either parentID or categoryID.
You can try this:
SELECT DISTINCT c.parentid, pc.productid
FROM products_categories pc
JOIN categories c on pc.categoryid = c.categoryid
WHERE c.parentid NOT IN (
SELECT DISTINCT categoryid FROM products_categories
)
Edit 1: doesnt account for all combos
SELECT DISTINCT c1.parentid, pc.productid
FROM products_categories pc
INNER JOIN categories c1 ON pc.categoryid = c1.categoryid
LEFT JOIN categories c2 ON c1.parentid = c2.categoryid
WHERE c2.categoryid IS NULL
To find what is in tbl_x but not in tbl_y, do this
SELECT ...
FROM tbl_x
LEFT JOIN tbl_y USING(...)
WHERE tbl_y.id IS NULL;
The construct IN ( SELECT ... ) is very poorly optimized. LEFT JOIN is well optimized (assuming suitable index).

Complex MySQL query with multiple select statements

I have three tables in Mysql that are link together:
Profile (ID, Name, Stuff..)
Contact(ID, ProfileID,desc,Ord)
Address(ID,ProfileID, desc, Ord)
Now I need to select all profile from the profile table, with the “desc” field from Contact and Address where Ord = 1. (this is for a search function where in a table I’ll display the name, main contact info and main Address of a client.
I can currently do this with three separate SQL request:
SELECT Name, ID FROM Profile WHERE name=”bla”
Then in a foreach loop, I’ll run the other two requests:
SELECT ProfileID, desc FROM Contact WHERE ProfileID=MyProfileID AND Ord=1
SELECT ProfileID, desc FROM Address WHERE ProfileID=MyProfileID AND Ord=1
I know you can do multiple SELECT in one query, is there a way I could group all three SELECT into one query?
You should be able to JOIN the tables on the profile.id and the profileid in the other tables.
If you are sure the profileid exists in all three tables, then you can use an INNER JOIN. The INNER JOIN returns matching rows in all of the tables:
select p.id,
p.name,
c.desc ContactDesc,
a.desc AddressDesc
from profile p
inner join contact c
on p.id = c.profileid
inner join address a
on p.id = a.profileid
where p.name = 'bla'
and c.ord = 1
and a.ord = 1
If you are not sure that you will have matching rows, then you can use a LEFT JOIN:
select p.id,
p.name,
c.desc ContactDesc,
a.desc AddressDesc
from profile p
left join contact c
on p.id = c.profileid
and c.ord = 1
left join address a
on p.id = a.profileid
and a.ord = 1
where p.name = 'bla'
If you need help learning JOIN syntax, here is a great visual explanation of joins
This query below only selects column when an ID from Profile table has atleast one match on tables: Contact and Address. If one or both of them are nullable, use LEFT JOIN instead of INNER JOIN because LEFT JOIN displays all records from the Left-hand side table regardless if it has a match on other tables or not.
SELECT a.*,
b.desc as BDESC,
c.desc as CDESC
FROM Profile a
INNER JOIN Contact b
ON a.ID = b.ProfileID
INNER JOIN Address c
ON a.ID = c.ProfileID
WHERE b.ORD = 1 AND
c.ORD = 1 AND
a.Name = 'nameHERE'
The LEFT JOIN version:
SELECT a.*,
b.desc as BDESC,
c.desc as CDESC
FROM Profile a
INNER JOIN Contact b
ON a.ID = b.ProfileID AND b.ORD = 1
INNER JOIN Address c
ON a.ID = c.ProfileID AND c.ORD = 1
WHERE a.Name = 'nameHERE'
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
i have created working demo as your requirement :
The query bellow will retrieve all matching records from the database.its retrieving profile id,name stufff and description of contact tables
select p.id,p.name,p.stauff,c.descr,a.descr from profile as p
inner join contact as c on c.profileid=p.id
inner join address as a on a.profileid=p.id
where p.name="bla" and c.ord=1 and a.ord=1

Mysql: How to get values from other table base from an id

Assuming that I have 3 tables on my database
Table:Category
category_id
Table:container
container_id
category_id
Table:products
container_id
product_id
How do you get all product_id based on a category_id?
For example I have these data inside my tables from above:
Table: category
sour
sweet
Bitter
Table: container
bottled
sachet
Table: product
sugar
vinegar
cocoa
How do you get all Product(tb_product) where category(tb_category) is sweet
You can use INNER JOIN to solve your problem.
SELECT a.*
FROM products a
INNER JOIN container b
on a.container_id = b.container_ID
INNER JOIN Category c
ON b.category_ID = c.categoryID
WHERE c.categoryName = 'sweete'
As you can see, I assume that the Category table has columns category_ID and categoryName. So in my example above,I've use category name to search for all products that belong on certain category.
select p.*
from Category c
join container c2 on c2.category_id = c.category_id
join product p on p.container_id = c2.container_id
where c.name = 'sweet';
Note the order of the tables in the query (which should perform faster than the other answer!)
you can use inner join like this:
SELECT a.product_id FROM products AS a INNER JOIN container AS b on a.container_id = b.container_Id INNER JOIN Category AS c on b.category_Id = c.category_id where c.category_id = 'new'