MYSQL issue - join not enough - mysql

i have 3 tables: products, buyers and ratings.
a buyer can rate i product and this will be saved as a row in ratings (with buyer_id and product_id) and a buyer can mark one product (at most one favorite product per buyer) as favorite (a binary flag in ratings).
i have a report of all buyers and want to add to each row in the report the name of the buyer's favorite product (if he has one), i tried this:
SELECT b.*, p.name
FROM buyers b, products p
LEFT JOIN ratings r
ON r.buyer_id = b.id
and r.product_id = p.id
and r.isFav=1
unfortunately this throws an error (Unknown column 'b.id' in 'on clause'). so i feel that i'm going the wrong way about it.
is there something that can help me accomplish what i need?

Without seeing the any data, one of the big problems is you are mixing JOIN syntax, I might rewrite it this way:
SELECT b.*, p.name
FROM buyers b
LEFT JOIN ratings r
ON b.id = r.buyer_id
LEFT JOIN products p
ON r.product_id = p.id
WHERE r.isFav=1

Related

How to handle multiple JOIN and LEFT JOINS in MySQL

I have 5 tables that need to be joined. These tables have to do with orders placed by customers and the orders turned into purchase orders for the relevant suppliers.
Table product_sale holds the customers products that they've ordered.
Table product holds the main information on those products.
Table sale_purchase is a bridging table between the sale and purchase order.
Note: This may or may not exist as the product might be out of stock and no purchase order was required.
Table product_purchase holds those linked products on the purchase order.
Table grn handles the receiving of those products.
Unfortunately in the customers sales order, I will need to access information from all of these tables. Here's the query I have so far:
SELECT
ps.*,
pp.received_qty,
p.group_ref,
p.subgroup_ref,
g.grn_id AS 'grn_ref',
g.grn_date
FROM
product_sale ps
INNER JOIN product p ON ps.product_ref = p.product_id
LEFT JOIN sale_purchase sp ON ps.sale_ref = sp.sale_ref
LEFT JOIN product_purchase pp ON pp.so_line_no = ps.line_no
LEFT JOIN grn g ON g.grn_id = pp.grn_ref
WHERE
ps.sale_ref = 150002
GROUP BY
line_no
ORDER BY
line_no
So far so good, although the received_qty for one line is wrong:
The first line's received qty should be 7 and not 4. I've checked the grn table and it definitely says 7. Can I please get some help as to where I am going wrong with this query? Also the grn_ref and grn_date should be NULL for line_no 1.00
Scrap it guys. I figured it out. I hadn't accounted for another purchase order that was in the system. Solution to the problem was adding AND pp.purchase_ref = sp.purchase_ref to the left join for product_purchase. See revised code below:
SELECT
ps.*,
pp.received_qty,
p.group_ref,
p.subgroup_ref,
g.grn_id AS 'grn_ref',
g.grn_date
FROM
product_sale ps
INNER JOIN product p ON ps.product_ref = p.product_id
LEFT JOIN sale_purchase sp ON ps.sale_ref = sp.sale_ref
LEFT JOIN product_purchase pp ON pp.so_line_no = ps.line_no AND pp.purchase_ref = sp.purchase_ref
LEFT JOIN grn g ON g.grn_id = pp.grn_ref
WHERE
ps.sale_ref = 150002
GROUP BY
line_no
ORDER BY
line_no

MySQL nested selection

I am trying to figure out this question on a practice page online with the following tables:
Question:
For all cases in which the same customer rated the same product
more than once, and in some point in time gave it a lower rating
than before, return the customer name, the name of the product,
and the lowest star rating that was given.
I cant seem to figure out why this isnt correct - would anyone be able to help?
Here is what I have so far (without sample data):
SELECT
Customer.customer_name,
Product.product_name,
MIN(Rating.rating_stars)
FROM Rating
JOIN Product ON Rating.prod_id = Product.prod_id
JOIN Customer ON Rating.cust_id = Customer.prod_id
GROUP BY Customer.customer_name, Product.product_name
HAVING COUNT(Product.prod_id) > 1
This query will return the minimum rating stars of a product that has been reviewed more than once by the same customer, with any of the newer ratings lower than an older rating:
SELECT
r1.prod_id,
r1.cust_id,
MIN(r1.rating_star) AS min_rating
FROM
rating r1 INNER JOIN rating r2
ON r1.prod_id=r2.prod_id
AND r1.cust_id=r2.cust_id
AND r1.rating_date>r2.rating_date
AND r1.rating_star<r2.rating_star
GROUP BY
r1.prod_id,
r1.cust_id
you can then join this query with products and customers table:
SELECT
customer.customer_name,
product.product_name,
m.min_rating
FROM (
SELECT
r1.prod_id,
r1.cust_id,
MIN(r1.rating_star) AS min_rating
FROM
rating r1 INNER JOIN rating r2
ON r1.prod_id=r2.prod_id
AND r1.cust_id=r2.cust_id
AND r1.rating_date>r2.rating_date
AND r1.rating_star<r2.rating_star
GROUP BY
r1.prod_id,
r1.cust_id) m
INNER JOIN customer on m.cust_id = customer.cust_id
INNER JOIN product ON m.product_id = product.product_id
Just a few points:
You cant specify the tables in the FROM clause the sane way you specify attributes in the SELECT. You can only have a single table in the FROM, and one more for each Join you use.
SELECT a, b, c FROM a; <----------fine
SELECT a, b, c FROM a, b; <----not fine
SELECT a, b, c FROM a JOIN b; <---fine
When it comes to the tables in the FROM/JOIN, you dont use "AS" to give them an alias, just the table name followed by the alias.
FROM atable a JOIN btable b; <--This assigns alias a to "atable" and b to "btable".
You also have to specify the common attribute that the tables are going to be joined on:
customer JOIN rating ON customer.cust_id = rating.cust_id;
As for the rest you can probably work out the correct WHERE clauses to use once you have the syntax down.

SQL Many to many to many

I'm having an issue with a query I have to build. There are 3 tables,all many to many after one another.
Table Stores - id,store_name
Table Clients - id,store_id,client_name
Table Products - id,client_id,product_name
In a few words - One Product can be bought from many Clients. And one Client can be in many Stores.
The task is to get all Stores with the number of their Clients (a person is a client who bought at least one product. If that client_id does not bought at least 1 Product - he is not a real Client).
SELECT store_name, COUNT(store_name)
FROM Stores s
INNER JOIN Clients c on (s.id = c.store_id )
INNER JOIN Products p on (c.id = p.client_id)
GROUP BY store_name
get a count of clients and then do a left join among the tables like below
select s.store_name,
t.client_count
from stores s left join
(
select c.store_id, count(p.id) as client_count
from Clients c left join products p
on c.id = p.client_id
group by p.client_id
) t on s.id = t.store_id

unknown column in on clause error

Hey guys I am this close in being able to represent the data that I wish to display
The column in question is a.state_Id.
I understand from the research I have done that the a.State_Id is out of scope. What is the missing piece to my sql statement?
If I take out the a.State_ID = c.State_Id leaving only the b.Product_Id = c.Product_Id then the data is displayed but I need to match against the state and product.
I know i need to add in another join somewhere but im not sure how. If anyone could help me that would be awesome!
SELECT a.state_id,
a.state_name,
a.state_abbreviatedname,
b.product_id,
b.product_name,
c.stateproduct_price
FROM states a,
products b
LEFT OUTER JOIN stateproducts c
ON a.state_id = c.state_id AND b.product_id = c.product_id
Update 1
The states table has been populated and contains the following fields:
State_Id
State_Name
State_AbbreviatedName
The Products table which has been populated to contain the base products. This table has the following fields:
Product_Id
Product_Name
Each state will have the same products however the price for each product changes with each state. This is why i have the StateProducts table. This table is empty and will be populated one by one by an interface I have created. The statesproducts table will have the following fields
State_Id //reference/relational field to be user for comparison
Product_Id //reference/relational field to be user for comparison
StateProduct_Price //new field
so i understand that i will receive NULL values in the price column.
I have been able to return a sort of cartesion product of the States and products table. However I now need to append the price for each combination on the right side of this cartesion table.
bearing in mind that stateproducts table is empty how would i accomplish this?
I guess you have a Many-to-Many relationship in which States can have multiple Products and Products can be on different States. So you need to join States into the mapping table StateProducts so you can be able to get the products on table Products.
SELECT a.State_ID,
a.State_Name,
a.State_AbbreviatedName,
b.Product_Id,
b.Product_Name,
c.StateProduct_Price
FROM States a
INNER JOIN StateProducts c
ON a.State_ID = c.State_Id
INNER JOIN Products b
ON b.Product_Id = c.Product_Id
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
UPDATE 1
use CROSS JOIN keyword,
SELECT a.state_id, a.state_name, a.state_abbreviatedname, b.product_id, b.product_name, c.stateproduct_price
FROM states a
CROSS JOIN products b
LEFT OUTER JOIN stateproducts c
ON a.state_id = c.state_id AND
b.product_id = c.product_id
from A to C, C to B.
By doing LEFT JOIN for each, you'll still end up with all possible states regardless of having actual state products.
SELECT
a.State_ID,
a.State_Name,
a.State_AbbreviatedName,
b.Product_Id,
b.Product_Name,
c.Product_Price
FROM
States a
LEFT JOIN StateProducts c
ON a.State_ID = c.State_Id
LEFT JOIN Products b
ON c.Product_Id = b.Product_Id
Try writing the from clause like this:
from StateProducts c join
States a
on c.state_id = c.state_id join
Products b
on c.product_id = b.product_id
For the Cartesian product (that is, to get all examples of states and products):
SELECT s.state_id, s.state_name, s.state_abbreviatedname,
p.product_id, p.product_name,
sp.stateproduct_price
from States s cross join
Products p left outer join
StateProducts sp
on sp.state_id = s.state_id and
sp.product_id = p.product_id

Need to use criteria from multiple tables to acquire data from multiple tables

I'm having a bit of a hiccup regarding a particular SQL query. I need to join data from two tables, while also limiting the data (but not necessarily grabbing it) by means of a third table. The tables are as follows:
MEMBERS(member_id,first_name,last_name)
MEMBERS_GROUPS(member_id,group_id)
CHARGES(charge_id,member_id,charge_amount,status)
I need to find all charges for all members of a specific group but I also want to grab the first and last name from the MEMBERS table. The query I've come up with thus far is:
select c.*, m.first_name, m.last_name
FROM charges c
LEFT JOIN member m
ON c.member_id=m.member_id
INNER JOIN members_groups mg
ON mg.group_id=1
i've also tried:
SELECT c.*, m.first_name, m.last_name
FROM charges c, members_groups mg, member m
WHERE c.member_id=mg.member_id
AND mg.group_id = 1
AND c.status='Valid'
AND c.member_id = m.member_id
…but neither returns the data I need. I'm sure I'm overthinking this, but I can't for the life of me get the correct values. I keep getting what appears to be the Cartesian product -- regardless, it's clearly returning too many rows and bad data.
Perhaps what you need is to also restrict the INNER JOIN on members_groups to
those rows with mg.member_id = m.member_id:
SELECT c.*, m.first_name, m.last_name
FROM charges c
LEFT JOIN member m
ON c.member_id=m.member_id
INNER JOIN members_groups mg
ON mg.group_id=1
AND mg.member_id = m.member_id