Combine left join with commas - mysql

So I've got this SQL code :
SELECT *
FROM A a1, AP ap1, P p1, M m1
LEFT JOIN F f1 ON f1.id_f = p1.f1
LEFT JOIN C c1 ON p1.c_id = c1.c_id
LEFT JOIN S s1 on p1.saison_id = s1.s_id
WHERE a1.a_type NOT IN (1)
AND a1.type_c IN (1,2)
AND ap1.a_id = a1.achats_id
AND p1.p_id = ap1.products_id
AND p1.m_id = m1.m_id
ORDER BY a_id ASC
And I get this error :
#1054 - Unknown column 'p1.f1' in 'on clause'
So I have no idea why doesn't it recognize p1.f1 on line 3. Any ideas?

You would simply use proper JOIN or INNER JOIN syntax:
SELECT *
FROM A a1 JOIN
AP ap1
ON ap1.a_id = a1.achats_id JOIN
P p1
ON p1.p_id = ap1.products_id JOIN
M m1
ON p1.m_id = m1.m_id LEFT JOIN
F f1
ON f1.id_f = p1.f1 LEFT JOIN
C c1
ON p1.c_id = c1.c_id LEFT JOIN
S s1
ON p1.saison_id = s1.s_id
WHERE a1.a_type NOT IN (1) AND a1.type_c IN (1, 2)
ORDER BY a1.a_id ASC;
Voila! Problem solved.

Related

OneToMany relationship. How to delete row?

Hello. I have just learned how to select all related data to, say, the row
tennismatch.ID = 1:
SELECT * FROM tennismatch m
JOIN tennismatch_tennisset ms
ON m.`ID` = ms.`TennisMatch_ID`
JOIN tennisset s
ON ms.`mapOfSets_ID` = s.`ID`
JOIN tennisset_game sg
ON s.`ID` = sg.`TennisSet_ID`
JOIN game g
ON sg.`gamesMap_ID` = g.`ID`
JOIN game_point gp
ON g.`ID` =gp.`Game_ID`
JOIN point p
ON gp.`points_ID` = p.`ID`
WHERE m.`ID` = 1
but I cannot figure how to delete it ALL.
Big thanks in advance.
Use a DELETE JOIN statement
DELETE m, s, g, p, ms, sg, gp
FROM tennismatch m
JOIN tennismatch_tennisset ms ON m.ID = ms.TennisMatch_ID
JOIN tennisset s ON ms.mapOfSets_ID = s.ID
JOIN tennisset_game sg ON s.ID = sg.TennisSet_ID
JOIN game g ON sg.gamesMap_ID = g.ID
JOIN game_point gp ON g.ID = gp.Game_ID
JOIN point p ON gp.points_ID = p.ID
WHERE m.ID = 1

Merging columns in INNER/LEFT JOIN returns NULL values?

I'm facing an issue where I have several tables joined, but I need to combine the e.url and f.url_new columns. Otherwise, the result is as expected. Here is my query.
SELECT a.`added_date`,b.`type`,c.`action`,e.`url`,f.`url_new`
FROM `websites_submitted_main` a
INNER JOIN `websites_submitted_type` b ON a.`type_id` = b.`type_id`
INNER JOIN `websites_submitted_action` c ON a.`action_id` = c.`action_id`
INNER JOIN `users` d ON a.`user_id` = d.`user_id`
LEFT JOIN `websites` e ON a.`url_id` = e.`id`
LEFT JOIN `websites_submitted_new` f ON a.`url_new_id` = f.`url_new_id`
WHERE a.`user_id` = 1 ORDER BY a.`added_date` DESC
I've tried CONCAT, but the column contained all NULL values. Here's that query.
SELECT a.`added_date`,b.`type`,c.`action`,CONCAT(e.`url`,f.`url_new`) AS url
FROM `websites_submitted_main` a
INNER JOIN `websites_submitted_type` b ON a.`type_id` = b.`type_id`
INNER JOIN `websites_submitted_action` c ON a.`action_id` = c.`action_id`
INNER JOIN `users` d ON a.`user_id` = d.`user_id`
LEFT JOIN `websites` e ON a.`url_id` = e.`id`
LEFT JOIN `websites_submitted_new` f ON a.`url_new_id` = f.`url_new_id`
WHERE a.`user_id` = 1 ORDER BY a.`added_date` DESC
Is there a minor modification I can make to this query to merge these columns?
try using CONCAT_WS() instead of CONCAT()
SELECT `CONCAT_WS(' ',e.url, f.url_new )` ....
CONCAT_WS() will concatenate the values if the values are not null.
From the manual of CONCAT()
CONCAT() returns NULL if any argument is NULL.
SELECT a.`added_date`,b.`type`,c.`action`,CONCAT_WS("", e.`url`,f.`url_new`) AS url
FROM `websites_submitted_main` a
INNER JOIN `websites_submitted_type` b ON a.`type_id` = b.`type_id`
INNER JOIN `websites_submitted_action` c ON a.`action_id` = c.`action_id`
INNER JOIN `users` d ON a.`user_id` = d.`user_id`
LEFT JOIN `websites` e ON a.`url_id` = e.`id`
LEFT JOIN `websites_submitted_new` f ON a.`url_new_id` = f.`url_new_id`
WHERE a.`user_id` = 1 ORDER BY a.`added_date` DESC

SQL query with deeper relationship

Table: user
Columns
- id
- username
- full_name
Table: pet
Columns
- id
- pet_name
- color_id
Table: pet_color
Columns
- id
- color
Table: results
Columns
- id
- user_id_1
- user_id_2
- user_id_3
- pet_id
- date
- some_text
This:
SELECT A.id, B.full_name, C.full_name, D.full_name, E.pet_name, A.date, A.some_text
FROM RESULTS AS A
LEFT OUTER JOIN USER AS B ON A.USER_ID1 = B.ID
LEFT OUTER JOIN USER AS C ON A.USER_ID2 = C.ID
LEFT OUTER JOIN USER AS D ON A.USER_ID3 = D.ID
LEFT OUTER JOIN PET AS E ON A.PET_ID = E.ID
will give me almost everything that I want except 'pet_color.color', but I can not figure it out what should I add to the query to get that too.
SELECT A.id, B.full_name, C.full_name, D.full_name, E.pet_name, A.date, A.some_text, pc.color
FROM RESULTS AS A
LEFT OUTER JOIN USER AS B ON A.USER_ID1 = B.ID
LEFT OUTER JOIN USER AS C ON A.USER_ID2 = C.ID
LEFT OUTER JOIN USER AS D ON A.USER_ID3 = D.ID
LEFT OUTER JOIN PET AS E ON A.PET_ID = E.ID
LEFT OUTER JOIN PET_COLOR PC on E.color_id = pc.id
SELECT A.id, B.full_name, C.full_name, D.full_name, E.pet_name, A.date, A.some_text, PC.color
FROM RESULTS AS A
LEFT OUTER JOIN USER AS B ON A.USER_ID1 = B.ID
LEFT OUTER JOIN USER AS C ON A.USER_ID2 = C.ID
LEFT OUTER JOIN USER AS D ON A.USER_ID3 = D.ID
LEFT OUTER JOIN PET AS E ON A.PET_ID = E.ID
LEFT OUTER JOIN PET_COLOR PC ON E.COLOR_ID = PC.ID
Not tried, but this should work
SELECT A.id, B.full_name, C.full_name, D.full_name, E.pet_name, A.date, A.some_text
FROM RESULTS AS A
LEFT OUTER JOIN USER AS B ON A.USER_ID1 = B.ID
LEFT OUTER JOIN USER AS C ON A.USER_ID2 = C.ID
LEFT OUTER JOIN USER AS D ON A.USER_ID3 = D.ID
LEFT OUTER JOIN PET AS E ON A.PET_ID = E.ID
INNER JOIN PET_COLOR PC ON PC.ID = E.COLOR_ID

INNER JOIN select columns from table2

How can I select certain columns from the second and third tables using INNER JOIN
SELECT
*
FROM
1_packages_plu AS p
INNER JOIN
1_stock as s ON p.fk_products_id = s.fk_products_id
AND branch = 1
INNER JOIN
1_products AS j ON p.fk_products_id = j.id
WHERE
fk_packages_id = 54;
In the tables 1_stock I only want to return the value of stock-repair columns and in the 1_products all I need is make,model columns
SELECT
p.* -- All columns from p
,
s.columnName -- Just that column from s
,
j.columnName -- And just that column from j
FROM
1_packages_plu AS p
INNER JOIN 1_stock as s
ON p.fk_products_id = s.fk_products_id
AND
branch = 1
INNER JOIN 1_products AS j
ON p.fk_products_id = j.id
WHERE
fk_packages_id = 54
You need (.) operator to access column:
SELECT
p.* ,
s.stock-repair,
j.make, j.model
FROM
1_packages_plu AS p
INNER JOIN 1_stock as s
ON p.fk_products_id = s.fk_products_id
AND branch = 1
INNER JOIN 1_products AS j
ON p.fk_products_id = j.id
WHERE
fk_packages_id = 54
ORDER BY p.colunmname
;
SELECT Table1.*, Table2.FK, Table2.SomeColumn, Table2.SomeColumn, Table3.SomeColumn, Table3.SomeColumn
FROM
Table1 INNER JOIN
Table2 ON Table1.FK = Table2.Table1FK INNER JOIN
Table3 ON Table2.FK = Table3.Table2FK

Error: Unknown Column in 'field list'

I receive an error that the column ls.amount is in field list when
I run the following query.
Can anyone help me diagnose the problem.
SELECT c.name, ic.keyword, COUNT(ic.keyword), SUM(ls.amount), ls.buyer FROM in_clicks AS ic
INNER JOIN ads AS a ON ic.ad_id = a.id
INNER JOIN ad_groups AS ag ON a.ad_group_id = ag.id
INNER JOIN campaigns AS c ON ag.campaign_id = c.id;
INNER JOIN leads AS l ON (ic.id = l.in_click_id)
INNER JOIN lead_status AS ls ON (l.id = ls.lead_id)
WHERE ic.create_date LIKE '%2011-08-19%' AND ic.location NOT LIKE '%Littleton%' AND discriminator LIKE '%AUTO_POST%'
GROUP BY ic.keyword ORDER BY COUNT(ic.keyword) DESC
The exact error message is:
Error Code: 1054
Unknown column 'ls.amount' in 'field list'
Drop the semicolon (;) on line 4. I suspect that is ending you query before you can define the ls alias.
SELECT c.name,
ic.keyword,
COUNT(ic.keyword),
SUM(ls.amount),
ls.buyer
FROM in_clicks AS ic
INNER JOIN ads AS a
ON ic.ad_id = a.id
INNER JOIN ad_groups AS ag
ON a.ad_group_id = ag.id
INNER JOIN campaigns AS c
ON ag.campaign_id = c.id
INNER JOIN leads AS l
ON ( ic.id = l.in_click_id )
INNER JOIN lead_status AS ls
ON ( l.id = ls.lead_id )
WHERE ic.create_date LIKE '%2011-08-19%'
AND ic.location NOT LIKE '%Littleton%'
AND discriminator LIKE '%AUTO_POST%'
GROUP BY ic.keyword
ORDER BY COUNT(ic.keyword) DESC