SQL query error unknown column in SELECT + JOIN + - mysql

This is my SQL code (using phpmyadmin simulator) :
SELECT user_activity.user_ip,user_activity.id,user_activity.post_id , posts.id
FROM user_activity,users
INNER JOIN posts ON user_activity.post_id = posts.id
WHERE user_activity.user_id = users.id and users.forgen_note = 'test note';
And it respons an error:
MySQL said: Documentation
#1054 - Unknown column 'user_activity_now.stream_id' in 'on clause'
How can I fix it please?

Don't mix join syntax:
SELECT user_activity.user_ip,user_activity.id,user_activity.post_id, posts.id
FROM user_activity
INNER JOIN users ON user_activity.user_id = users.id
INNER JOIN posts ON user_activity.post_id = posts.id
WHERE users.forgen_note = 'test note';
Generally you should avoid using comma joins. It's harder to read and is less expressive overall about what you're doing.

Related

Join three tables with group by giving error

There are three tables in the database users, organization_entries and user_invoices and I am trying to Join these three tables and my query is somewhat like this
select users.id , sum(user_invoices.due_amount) , organization_entries.id, organization_entries.createdAt from users INNER JOIN user_invoices ON users.id = user_invoices.customer_id INNER JOIN on users.id = organization_entries.user_id GROUP BY users.id ORDER BY organization_entries.createdAt;
But again and again, I am getting this error -
ERROR 1064 (42000): 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 'on users.id = organization_entries.user_id GROUP BY users.id ORDER BY organiza' at line 1
I am not able to understand where I am doing things wrong.
Update the query as follows, you are missing table in second inner join
select users.id , sum(user_invoices.due_amount) , organization_entries.id,
organization_entries.createdAt
from users INNER JOIN user_invoices ON users.id = user_invoices.customer_id
INNER JOIN organization_entries ON users.id = organization_entries.user_id
GROUP BY users.id ORDER BY organization_entries.createdAt ;

MySQL LEFT JOIN error 1064 when querying Wordpress database

I'm trying to retrieve some data from a Wordpress database:
SELECT M.meta_value,wp_users.ID
FROM wp_postmeta AS M
WHERE meta_key = "_from_email" AND post_id = 277124
LEFT JOIN wp_users ON M.meta_value = wp_users.user_email
Here, my intention is to get the ID of the wp_users user who has an email identical to one in meta_value.
But I get the following error:
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 'LEFT JOIN wp_users ON M.meta_value = wp_users.user_email
Do you see any syntax error?
left join must be declare before where clause
SELECT M.meta_value,wp_users.ID
FROM wp_postmeta AS M
LEFT JOIN wp_users ON M.meta_value = wp_users.user_email
WHERE meta_key = "_from_email" AND post_id = 277124
be sure you are not using column for left joined table in where clause otherwise this implies that the join work as an inner join .. eventually move these column in the related ON clause

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.

Inner Join to get all attributes

I have 2 tables :
albums (idalbum, idauthor, idcompositor, idfeat)
people (id, firstname, last name)
My current Query :
SELECT * FROM albums where idalbum=:id
INNER JOIN people ON albums.idauthor = people.id
INNER JOIN people ON albums.idcompositor = people.id
INNER JOIN people ON albums.idfeat = people.id
What I want to do with my query :
[Album], [Author[First Name, Last Name]], [Compositor[First Name, Last Name]], [Feat[First Name, Last Name]]
My problem:
I have an error : SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;
You need to correct your query where clause should be used after your join statements and also you are not using proper table names, according to structure shown in question you can write your query as
SELECT * FROM albums a
INNER JOIN people p ON a.idauthor = p.id
INNER JOIN people p1 ON a.idcompositor = p1.id
INNER JOIN people p2 ON a.idfeat = p2.id
where a.idalbum=:id

MySQL Syntax error

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.