MYSQL- select and join multiple tables into one JSON - mysql

Can you please advise how I can fix this query?
I have 3 DB Tables.
networks.
products.
comments.
The query looked like this before I added the "comments" table (worked as expected):
SELECT networks.*, products.product, products.type
FROM products
JOIN networks ON products.id=networks.product_Id
and now I tried modifing it with the additional table, and it doesn't work:
SELECT
networks.*,
products.product,
products.type,
comments.id AS comment_Id,
comments.newLine,
comments.lineComment AS comment,
comments.topComment,
comments.bottomComment
JOIN networks ON products.id=networks.product_Id,
ON comments.id=networks.comment_Id
How can I fix this query?
Thanks for the helpers :)

You joined networks table with condition products.id=networks.product_Id
But for comments table you specified the condition comments.id=networks.comment_Id but you forgot to join the comments table.
try
SELECT networks.*,
products.product,
products.type,
comments.id AS comment_Id,
comments.newLine,
comments.lineComment AS comment,
comments.topComment,
comments.bottomComment
FROM products
JOIN networks ON products.id=networks.product_Id
JOIN comments ON comments.id=networks.comment_Id

I think that this is the syntax you are after:
SELECT
n.*,
p.product,
p.type,
c.id AS comment_id,
c.newLine,
c.lineComment AS comment,
c.topComment,
c.bottomComment
FROM products p
INNER JOIN networks n ON p.id = n.product_id
INNER JOIN comments c ON c.id = n.comment_id
That is: the syntax for multiple joins is FROM ... JOIN ... ON ... JOIN ... ON ....
Note that the use of table aliases shortens the query and makes it easier to read and write.

Related

Using two inner join tables

I have come up with two queries, both use an inner join on two different tables.
Query 1
SELECT PRODUCTS.CODE, PRODUCTS.REFERENCE, PRODUCTS.TAXCAT, PRODUCTS.DISPLAY,PRODUCTS.NAME, PRODUCTS.PRICEBUY, PRODUCTS.PRICESELL, CATEGORIES.NAME AS CATEGORY
FROM PRODUCTS INNER JOIN CATEGORIES ON PRODUCTS.CATEGORY = CATEGORIES.ID;
Query 2
SELECT PRODUCTS.CODE, PRODUCTS.REFERENCE, PRODUCTS.TAXCAT, PRODUCTS.DISPLAY,PRODUCTS.NAME, PRODUCTS.PRICEBUY, PRODUCTS.PRICESELL,STOCKCURRENT.UNITS AS UNIT FROM PRODUCTS INNER JOIN STOCKCURRENT ON STOCKCURRENT.PRODUCT = PRODUCTS.ID;
Both queries run fine on their own, when I try to use both inner joins together I get errors. This is what I came up with on my own. I'm having trouble understanding the syntax to achieve this.
SELECT PRODUCTS.CODE, PRODUCTS.REFERENCE, PRODUCTS.TAXCAT,
PRODUCTS.DISPLAY,PRODUCTS.NAME, PRODUCTS.PRICEBUY,
PRODUCTS.PRICESELL,STOCKCURRENT.UNITS AS UNIT FROM PRODUCTS INNER JOIN
STOCKCURRENT ON STOCKCURRENT.PRODUCT = PRODUCTS.ID, CATEGORIES.NAME AS
CATEGORY FROM PRODUCTS INNER JOIN CATEGORIES ON PRODUCTS.CATEGORY =
CATEGORIES.ID;
Thank you.
Your attempted query has several syntax problems. Assuming you just want to join together the three tables, you may try the following query:
SELECT
p.CODE,
p.REFERENCE,
p.TAXCAT,
p.DISPLAY,
p.NAME,
p.PRICEBUY,
p.PRICESELL,
s.UNITS AS UNIT,
c.NAME AS CATEGORY
FROM PRODUCTS p
INNER JOIN STOCKCURRENT s
ON s.PRODUCT = p.ID
INNER JOIN CATEGORIES c
ON p.CATEGORY = c.ID;
Note that I introduced table aliases here. These aliases can be used elsewhere in the query to avoid having to repeat the entire table name.
By the way, I can also see taking a union of your two original queries. But without expected output, it was not entirely clear what you want.

Easy SQL Query - Aligning foreign keys

I'm taking longer than I expected with an easy Query in MySQL. I think it's gonna be a nested query but I don't see it easily.
I have 3 tables: Users, Comments, and Businesses. Comments have business_id, and user_id as foreign keys.
So I want the result of users.name and comments.review, having the number of the business.
So my First (and wrong) attempt was:
SELECT users.name, users.image, comments.review
FROM reviews JOIN users JOIN businesses
WHERE reviews.user_id=users.id AND reviews.business_id=4;
I want to set that PrimaryKey.user_id is equal to ForeignKey.users.id.
From all of the comments, I want to take these which are from the business_id=4.
It gives me failiure with both 'WHERE' clauses. So not sure if I could fix this with a nested query or maybe with a JOIN clause?
Any help will be appreciated!
Thank you all. [Edited Query]
Try this out and let me know in case of any queries.
select c.name,c.image,a.review
from
comments a
inner join
(select * from buisnesses where buisness_id = 4) b
on a.buisness_id = b.buisness_id
inner join
users c
on a.user_id = c.user_id;
or
select b.name,b.image,a.review
from
comments a
inner join
users b
on a.user_id = b.user_id
where a.buisness_id = 4;
Give this a try:
SELECT
users.name,
users.image,
comments.review
FROM reviews
JOIN users
ON reviews.user_id = users.id
JOIN businesses
ON reviews.business_id = business.business_id
WHERE reviews.business_id=4;
Since you're not using any of the columns from the business table, you could probably drop it from the query:
SELECT
users.name,
users.image,
comments.review
FROM reviews
JOIN users
ON reviews.user_id = users.id
WHERE reviews.business_id=4;

SQL Nested Inner Joins

Im trying to use the results of an inner join in another query and having trouble getting my head around how.
This is the first query i am running:
SELECT projects.Project_ID, projects.Name
FROM projects
INNER JOIN assigned_users
ON assigned_users.Project_ID=projects.Project_ID AND assigned_users.User_ID=4;
This is getting all of the assigned projects for a particular user. The Project_ID's this query returns i want to use to find all the related requirements for those projects.
SELECT *
FROM requirements
WHERE requirements.Project_ID=1;
So instead of finding the requirements for project '1' i want to get the requirements for all projects assigned to a particular user. Any help would be appreciated.
Cheers
If I understand correctly, you would just add another JOIN:
SELECT . . .
FROM assigned_users au JOIN
projects p
ON au.Project_ID = p.Project_ID JOIN
requirements r
ON r.Project_ID = p.Project_ID
WHERE au.User_ID = 4;
To the existing query, we can just add:
JOIN requirements
ON requirements.Project_ID = projects.Project_ID
We add expressions to the SELECT list to retrieve values of columns from requirements.
SELECT projects.Project_ID, projects.Name
, requirements.some_column
, requirements.some_other_column
It's likely that we also want to add an ORDER BY clause so that the rows are returned in a predictable sequence.
Note that if there are no matching rows in requirements for a given Project_ID, then that project will not be returned. To return those rows, we can specify an outer join.
Current query:
SELECT projects.Project_ID
, projects.Name
FROM projects
JOIN assigned_users
ON assigned_users.Project_ID=projects.Project_ID
AND assigned_users.User_ID=4
Proposed query with outer join to requirements table:
SELECT projects.Project_ID
, projects.Name
, requirements.Requirement_ID
, requirements.Requirement_Name
FROM projects
JOIN assigned_users
ON assigned_users.Project_ID=projects.Project_ID
AND assigned_users.User_ID=4
LEFT
JOIN requirements
ON requirements.Project_ID = projects.Project_ID
ORDER
BY projects.Project_ID
, requirements.Requirement_ID
Equivalently, we could relocate the condition assigned_users.User_ID=4 from the ON clause of the inner join to a WHERE clause, before the ORDER BY.
You can use either:
SELECT *
FROM requirements req
INNER JOIN projects pro
ON req.Project_ID = pro.Project_ID
INNER JOIN assigned_users u
ON u.Project_ID=pro.Project_ID
WHERE u.User_ID=4;
or
SELECT *
FROM requirements
WHERE requirements.Project_ID=(
SELECT Project_ID
FROM projects
INNER JOIN assigned_users
ON assigned_users.Project_ID=projects.Project_ID AND assigned_users.User_ID=4;
);

"Not unique table/alias" in MySQL

The code below gives me Not unique table/alias: 'presentations'
I've read several articles about this and I'm pretty sure the solution is to use aliases for my tables. I just can't understand how I create aliases or where I should add them.
Could someone please try to explain it for me?
Thanks!
SELECT categories.name, categories.sub_name, parts.name
FROM categories
INNER JOIN presentations
ON categories.id=presentations.category
INNER JOIN presentations
ON parts.id=presentations.parts
WHERE presentations.id=5;
After table name or field name you can give alias name and you can use it.
SELECT C.name, C.sub_name, PT.name
FROM categories C
INNER JOIN presentations P
ON C.id=P.category
INNER JOIN parts PT
ON PT.id=P.parts
WHERE P.id=5;
In above example C,PT,P is alias name of categories,presentation,parts table respectively.
You are joining the same table 2 times and hence you need to provide the unique alias name. However looks like you are looking for parts table and hence need to join that table
SELECT
categories.name,
categories.sub_name,
parts.name
FROM categories
INNER JOIN presentations ON categories.id=presentations.category
INNER JOIN parts ON parts.id=presentations.parts
WHERE presentations.id=5;
For better readability you can always give some short alias name something as
select
c.name,
c.sub_name,
p.name as parts_name
from categories c
join presentations pr on pr.category = c.id
join parts p on p.id = pr.parts
where pr.id = 5

How to left join multiple one to many tables in mysql?

i have a problem with joining three tables in mysql.
lets say we have a table named posts which I keep my entries in it, i have a table named likes which i store user_id's and post_id's in and a third table named comments which i store user_id's and post_id's and comment's text in it.
I need a query that fetches list of my entries, with number of likes and comments for each entry.
Im using this query:
SELECT posts.id, count(comments.id) as total_comments, count(likes.id) as total_likes
FROM `posts`
LEFT OUTER JOIN comments ON comments.post_id = posts.id
LEFT OUTER JOIN likes ON likes.post_id = posts.id
GROUP BY posts.id
but there is a problem with this query, if comments are empty for an item, likes count is just ok, but lets say if an entry has 2 comments and 4 likes, both total_comments and total_likes will be "8", meaning that mysql multiplies them.
I'm confused and I dont know what whould I do.
Thanks in advace.
Use count(distinct comments.id) and count(distinct likes.id), provided these ids are unique.
Well this is one way to approach it (assuming mysql allows derived tables):
SELECT posts.id, comments.total_comments, likes.total_likes
FROM `posts`
LEFT OUTER JOIN (select post_id, count(id) as total_comments from comments) comments
ON comments.post_id = posts.id
LEFT OUTER JOIN (select post_id, count(id) as total_likes from likes) likes
ON likes.post_id = posts.id
You could also use correlated subqueries. You may want a case statment inthere to account for putting in a 0 when there are no matched records.
Let's try a correlated subquery:
SELECT posts.id,
(select count(Id) from comments where post_id = posts.id) as total_comments,
(select count(Id) from likes where post_id = posts.id) as total_likes
FROM `posts`