MySQL Syntax error - mysql

What's wrong with this code?
FROM product_tag, ps_product_tags_all
LEFT JOIN users ON
users.id = product_tag.lang
LEFT JOIN images ON
images.id = ps_product_tags_all.lang
Error:
Unknown column 'product_tag.lang' in 'on clause'

You are mixing implicit and explicit joins and joining in the wrong order. Try this:
SELECT *
FROM ps_product_tags_all
LEFT JOIN images ON
images.id = ps_product_tags_all.lang, product_tag
LEFT JOIN users ON
users.id = product_tag.lang
WHERE ...
Remember that explicit JOIN has higher precedence than the implicit join from using comma. To avoid this error I would recommend that you always use explicit joins:
SELECT *
FROM ps_product_tags_all
LEFT JOIN images ON images.id = ps_product_tags_all.lang
LEFT JOIN product_tag ON ...
LEFT JOIN users ON users.id = product_tag.lang

This is not syntax error, but structure error - you don't have "lang" column in "product_tag" table.

Related

In MySQL JOIN means which join perform..?

It's to confusing, I have query where I have write JOIN with multiple table then which type of join it'll perform..?
For example :
SELECT
b.*
FROM
tbl_bookings b
JOIN tbl_users ua ON ua.id = b.act_id
JOIN tbl_users uc ON uc.id = b.cust_id
JOIN tbl_venue v ON b.venue_id = v.venue_id
WHERE
b.act_id = 4
Can any one please let me know by which type of join it'll perform..?
JOIN equals to an INNER JOIN . They are functionally equivalent, but INNER JOIN can be a bit clearer to read, especially if the query contains other type of JOIN
For something other than INNER JOIN you should specify the join you want.
LEFT JOIN / RIGHT JOIN which are the same as LEFT OUTER JOIN and RIGHT OUTER JOIN .
Those are just different ways of saying the same thing.
It will perform INNER JOIN. It is good to write INNER JOIN when you have different types of joins in query.

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.

outer join more than 2 tables in access

After following this thread. I am getting an error as "Syntax Error in JOIN Operation"
Here Is my query
SELECT
StudentMaster.Student_id,
ExamDetails.Subject_id,
SubjectMaster.Subject_id
FROM
(StudentMaster
LEFT OUTER JOIN ExamDetails
ON StudentMaster.Student_id = ExamDetails.Student_id)
LEFT OUTER JOIN ExamDetails
ON SubjectMaster.subject_id = ExamDetails.Subject_id
Any help regarding this would be appreciable
Have you tried this one.
select s.Student_id,e.Subject_id,sub.Subject_id
from YourDatabaseName.SubjectMaster as sub
left outer join YourDatabaseName.ExamDetails as e
on sub.Subject_id=e.Subject_id
left outer join YourDatabaseName.StudentMaster as s
on s.Student_id=e.Student_id
group by s.Student_id
order by s.Student_id;

Why is mysql saying there is a syntax error?

I'm pretty new to the world of outer joins, and I'm trying to get used to them, but I have no idea why mysql is saying the syntax is incorrect. Does anyone care to give some insight?
SELECT * FROM user_courses, course_updates
WHERE user_courses.crn = course_updates.crn AND user_courses.user_id = 1
LEFT JOIN followers ON (followers.followee = course_updates.user_id)
The LEFT JOIN clause should become before the WHERE clause.
SELECT * FROM user_courses, course_updates
LEFT JOIN followers ON (followers.followee = course_updates.user_id)
WHERE user_courses.crn = course_updates.crn AND user_courses.user_id = 1
By the way, you can also use `INNER JOIN for you other tables, so you don't have two types of syntax:
SELECT * FROM user_courses
INNER JOIN course_updates on user_courses.crn = course_updates.crn
LEFT JOIN followers ON followers.followee = course_updates.user_id
WHERE user_courses.user_id = 1
Note that I have omitted the parentheses around the ON condition, which is perfectly valid. Also, you can see that using INNER JOIN, you can specify the join conditions in the join itself, leaving the WHERE clause solely for filtering. I think this results in better readability of your query.
Use like this
SELECT * FROM user_courses, course_updates
LEFT JOIN followers ON (followers.followee = course_updates.user_id)
WHERE user_courses.crn = course_updates.crn AND user_courses.user_id = 1
Because there is a syntax error.
Try
SELECT *
FROM user_courses LEFT JOIN
course_updates ON user_courses.crn = course_updates.crn
AND user_courses.user_id = 1 LEFT JOIN
followers ON (followers.followee = course_updates.user_id)
From enter link description here
the FROM table_references comes before the WHERE where_condition
Also maybe look at JOIN Syntax
Try This..
SELECT * FROM user_courses INNER JOIN course_updates ON user_courses.crn = course_updates.crn LEFT JOIN followers ON followers.followee = course_updates.user_id WHERE user_courses.user_id = 1
Since, WHERE clause will be used after JOIN
Yes your syntax is wrong, in your case use WHERE clause at the end of the statement as follows:
SELECT *
FROM user_courses uc
INNER JOIN course_updates cu ON cu.crn = uc.crn
LEFT OUTER JOIN followers f ON f.followee = cu.USER_ID
WHERE uc.USER_ID = 1
Also make sure f.followee type and cu.USER_ID are the same.
Hope this would help you!!!

SQl queries left join not giving accurate results

I have been trying to join two tables (USERS AND USERS_ROLES) based on their role id I put the left join on following query
users.id = users_roles.fk_user_id
but the output is not correct of users_roles.fk_role_id coulmun and shows NULL where it should display the id of users.id = users_roles.fk_user_id that is 4 (at most places) because on users.id = users_roles.fk_user_id the value of users_roles.fk_role_id = 4
Kindly let me know how can i fix that so my query should result the exact vlaues of ids where they match,
Thanks
SELECT users.id, users.v_first_name, users.v_last_name, user_facility.fk_facility_id,users.fk_tenant_id, marital_status.v_marital_status,
users.v_blood_type, NOW(),users_roles.fk_role_id
FROM users
LEFT JOIN (user_facility, marital_status, users_roles) ON
users.id = user_facility.fk_user_id AND users.fk_marital_status_id=marital_status.id AND users.id = users_roles.fk_user_id
Usage of AND operator when used with Left or Right join gives different result. You should be clear what you are trying to accomplish..See this
well it is what you get by first implicitly inner-joining 3 tables and then explicitly left-joining the result to a 4th table only if 3 conditions relevant to all of the 3 inner-joinded tables are matched (i.e. when 3rd condition is false, nothing is joined from either of the 2 remaining tables)
i strongly suggest not to combine implicit and explicit joins, i personally use explicit joins all the time:
if you need an outer join:
SELECT ...
FROM users
LEFT JOIN user_facility ON users.id = user_facility.fk_user_id
LEFT JOIN marital_status ON users.fk_marital_status_id=marital_status.id
LEFT JOIN users_roles ON users.id = users_roles.fk_user_id
if you need an inner join:
SELECT ...
FROM users
JOIN user_facility ON users.id = user_facility.fk_user_id
JOIN marital_status ON users.fk_marital_status_id=marital_status.id
JOIN users_roles ON users.id = users_roles.fk_user_id
or if you prefere implicit inner joins for some obscure reason:
SELECT ...
FROM users,
user_facility,
marital_status,
users_roles
WHERE users.id = user_facility.fk_user_id
AND users.fk_marital_status_id=marital_status.id
AND users.id = users_roles.fk_user_id
(implicit outer joins are getting deprecated in all RDBMS as far as i know)
When it shows NULL it means there isn't a correspondency (relation) between all tables in the JOIN clause.
If you want to show only the ones that have relations in all tables, use INNER JOIN instead.
SELECT u.id,
u.v_first_name,
u.v_last_name,
uf.fk_facility_id,
u.fk_tenant_id,
ms.v_marital_status,
u.v_blood_type,
NOW(),
ur.fk_role_id
FROM users u
INNER JOIN user_facility uf ON u.id = uf.fk_user_id
INNER JOIN marital_status ms ON u.fk_marital_status_id=ms.id
INNER JOIN users_roles ur ON u.id = ur.fk_user_id