Error in JOIN while using Common Table Expresion - mysql

I am using the expression below to find out which 'resource' type customers have 'work_resource' that are active.
WITH cte_ss AS (SELECT wr.user_id
FROM work w
JOIN work_resource wr ON wr.work_id = w.id
WHERE wr.work_resource_status_type_code = 'active'
),
SELECT u.uuid
FROM user u
JOIN company c ON c.id = u.company_id
LEFT JOIN cte_ss on cte_ss.user_id = u.id
AND c.customer_type = 'resource'
White trying to run this, I get the following error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT u.uuid
FROM user u
JOIN company c ON c.id = u.company_id
LEFT JOIN cte_ss' at line 6
Both the individual queries (without the LEFT JOIN) are working, so not sure what I am doing wrong here

A cte would look like this
But as i don't know nothing about your tables, you have to figure it out yourself
WITH cte_ss AS (
SELECT
user_id
FROM work w
JOIN work_resource wr ON wr.work_id = w.id
WHERE wr.work_resource_status_type_code = 'active'
)
SELECT u.uuid
FROM user u
JOIN company c ON c.id = u.company_id
LEFT JOIN cte_ss on cte_ss.user_id = u.id
AND c.customer_type = 'resource'

Related

DELETE with multiple JOIN in MySQL?

I have a SELECT query with multiple JOIN who works perfectly, but now I want to DELETE this rows, and I can't find how.
This code returns me an error SQL (1064):
DELETE FROM rapport
INNER JOIN course ON rapport.course_id = course.id
INNER JOIN reunion ON course.reunion_id = reunion.id
INNER JOIN paris ON rapport.paris_id = paris.id
WHERE reunion.id = 231431
AND paris.bookmaker_id = 3
I need some help, thanks
Your syntax is wrong.
Use this with aliases also:
DELETE r
FROM rapport r
INNER JOIN course c ON r.course_id = c.id
INNER JOIN reunion u ON c.reunion_id = u.id
INNER JOIN paris p ON r.paris_id = p.id
WHERE u.id = 231431 AND p.bookmaker_id = 3
If you want to delete from all tables then you must specify their aliases after DELETE:
DELETE r, c, u, p
FROM rapport r
INNER JOIN course c ON r.course_id = c.id
INNER JOIN reunion u ON c.reunion_id = u.id
INNER JOIN paris p ON r.paris_id = p.id
WHERE u.id = 231431 AND p.bookmaker_id = 3
Basically, you need to specify what table to delete from. Assuming this is rapport:
DELETE r
FROM rapport r JOIN
course c
ON r.course_id = c.id JOIN
reunion ru
ON c.reunion_id = ru.id JOIN
paris p
ON r.paris_id = p.id
WHERE ru.id = 231431 AND p.bookmaker_id = 3;
MySQL also supports deletion from multiple tables. However, cascading constraints are usually a better approach.

How to use Left join?

i get an error got when i put this is my phpmyadmin sql,
error code #1052.
SELECT *
FROM users u
LEFT
JOIN user_tokens t
ON u.userid = u.id
LEFT
JOIN jt_campings_users cu
ON cu.userid = u.id
LEFT
JOIN campings c
ON c.user_id = u.id
WHERE id = 9271
You will have to give query hint to optimizer to where id is coming from because that might be possibility id is available in one or more tables :
WHERE u.id = 9271
So, the best practise to avoid such error is to use alias :
SELECT <column list> -- Do not use `*`, always qualify column list
FROM `users` u LEFT JOIN -- i removed LEFT JOIN user_tokens t as no mapping available
`jt_campings_users` jt
ON jt.userid = u.id LEFT JOIN
`campings` c
ON c.user_id = users.id
WHERE u.id = 9271;

left join, inner join, one before the other, other issue?

I have a form and MySQL that works with this:
$sql = "
SELECT p.*
, t.*, f.name AS fname
FROM table_posts p
JOIN table_topics t
USING(tid)
JOIN table_forums f
ON f.fid = t.fid
WHERE f.fid IN($forums)";
I wish to add an option on the form so that any tid's in the subscriptions table for the user are excluded from the results from the sql above:
$sql .= " LEFT JOIN (SELECT x.tid FROM table_subscriptions AS x WHERE x.username = '$user' AND x.type = 'x' GROUP BY x.tid) AS query2 USING (tid) AND query2.tid IS NULL";
however, am encountering
MySQL encountered the following error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LEFT JOIN (
Is the issue because the left join should be done first, or is there some other issue? Any clues for corrections appreciated.
You can bring in the subscriptions table with a LEFT JOIN, and then filter on unmatched records in the WHERE clause
SELECT
p.*,
t.*,
f.name AS fname
FROM table_posts AS p
INNER JOIN table_topics AS t USING (tid)
INNER JOIN table_forums AS f ON f.fid = t.fid
LEFT JOIN table_subscriptions AS s ON s.username = ? AND s.type = 'x' AND s.tid = p.tid
WHERE
f.fid IN (?, ?, ?)
AND s.tid IS NULL
Note: I updated your query so it uses query parameters instead of concatenating variables in the SQL string: this is both more efficient and secure.
You can also use NOT EXISTS:
SELECT
p.*,
t.*,
f.name AS fname "
FROM table_posts AS p
INNER JOIN table_topics AS t USING (tid)
INNER JOIN table_forums AS f ON f.fid = t.fid
WHERE NOT EXISTS (
SELECT 1
FROM table_subscriptions AS s
WHERE s.username = ? AND s.type = 'x' AND s.tid = p.tid
)
WHERE f.fid IN (?, ?, ?)

Correct syntax for COUNT(DISTINCT...)

I've got the following SQL query and I'm trying to implement pagination, so I first want to get the COUNT of the result:
The normal query (works fine)
SELECT DISTINCT c.*, p1.*, username FROM candidate c
LEFT JOIN note p1 ON (c.candID = p1.candidateID)
LEFT JOIN user ON p1.userID = user.id
LEFT OUTER JOIN note p2 ON
(c.candID = p2.candidateID AND (p1.noteID < p2.noteID))
WHERE p2.candidateID IS NULL ORDER BY c.firstname ASC
I've tried the following, but it throws an error and I'm not sure what correct syntax to use:
Attempting to count the results (doesn't work)
SELECT COUNT(DISTINCT c.*, p1.*, username) FROM candidate c
LEFT JOIN note p1 ON (c.candID = p1.candidateID)
LEFT JOIN user ON p1.userID = user.id
LEFT OUTER JOIN note p2 ON
(c.candID = p2.candidateID AND (p1.noteID < p2.noteID))
WHERE p2.candidateID IS NULL ORDER BY c.firstname ASC
The error:
Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ', p1., username) FROM candidate c LEFT ' at line 1
One option is to use a subquery:
SELECT COUNT(*)
FROM (
SELECT DISTINCT c.*, p1.*, username FROM candidate c
LEFT JOIN note p1 ON (c.candID = p1.candidateID)
LEFT JOIN user ON p1.userID = user.id
LEFT OUTER JOIN note p2 ON
(c.candID = p2.candidateID AND (p1.noteID < p2.noteID))
WHERE p2.candidateID IS NULL
) t
Depending on your data, you may be able to do this without the subquery, but you cannot use multiple columns with the count aggregate -- that's what is causing your error.

MYSQL query using Left Join and Where IN clause

I have three tables A B C and i'm trying to retrieve information from all three.
A has the columnns userid avatar username and B has the column postid, dateshared and C has the column commenter postid datecommented.
I'm trying to run the query
Select C.comment, C.commenter, C.datecommented, B.postid, B.dateshared A.username A.avatar from B Left Join C Left join A on C.postid = B.postid AND A.userid = C.commenter where B.postid IN ('1','2','3') order by C.dateshared desc
but it gives the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where B.postid IN ('1', '2', '3') order by C.dateshared '
Can anyone point out what I'm doing wrong or suggest how to go about it?
Each LEFT JOIN requires its own ON condition:
SELECT C.comment, C.commenter, C.datecommented, B.postid, B.dateshared, A.username A.avatar
FROM B
LEFT JOIN
C
ON C.postid = B.postid
LEFT JOIN
A
ON A.userid = C.commenter
WHERE B.postid IN ('1','2','3')
ORDER BY
C.dateshared desc
This should work for you, your query had some syntax errors:
Select C.comment,C.commenter,C.datecommented,B.postid,B.dateshared,A.username,A.avatar
from B
Left Join C on C.postid = B.postid
Left join A on A.userid = C.commenter
where B.postid IN ('1','2','3')
order by C.dateshared desc