MySQL LEFT JOIN error 1064 when querying Wordpress database - mysql

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

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 ;

SQL syntax error INNER JOIN

I'm trying to run this sql query and keeps getting this 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 'INNER JOIN wp_posts ON wp_postmeta.post_id = wp_posts.ID WHERE
wp_posts.post_typ' at line 3 (Line 4)
My sql query is this:
UPDATE wp_postmeta
SET meta_value = "1316"
INNER JOIN wp_posts ON wp_postmeta.post_id = wp_posts.ID
WHERE wp_posts.post_type="imagen_dia" AND wp_postmeta.meta_key="ae_post_template"
Any help please?
In mysql the JOIN should be before the set
UPDATE wp_postmeta
INNER JOIN wp_posts ON wp_postmeta.post_id = wp_posts.ID
SET meta_value = "1316"
WHERE wp_posts.post_type="imagen_dia" AND wp_postmeta.meta_key="ae_post_template"
The syntax for joined updates is vendor-specific. So do this without a join to have the update statement simple, readable and safe :-)
UPDATE wp_postmeta
SET meta_value = '1316'
WHERE meta_key = 'ae_post_template'
AND post_id IN (SELECT id FROM wp_posts WHERE post_type = 'imagen_dia');
This statement is standard SQL and should work in about every RDBMS.
You are missing the FROM word:
UPDATE wp_postmeta
SET meta_value = "1316"
FROM wp_postmeta
INNER JOIN wp_posts ON wp_postmeta.post_id = wp_posts.ID
WHERE wp_posts.post_type="imagen_dia" AND wp_postmeta.meta_key="ae_post_template"

SQL query error unknown column in SELECT + JOIN +

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.

No results showing up AND error as well with where clause

So i am trying to get values that ONLY have name in role as admin
When i put the WHERE command right after the FROM command i get an error that says
#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 'INNER JOIN `users` AS `u` ON u.uid = ur.uid INNER JOIN `role` AS `r` ON r.rid = ' at line 9
But when i put it in the end of the entire thing likt it is below i get no error BUT i dont get any results EITHER!
SELECT f.field_first_name_value,
l.field_last_name_value,
r.name,
u.name,
u.mail,
u.created
FROM `users_roles` AS `ur`
INNER JOIN `users` AS `u` ON u.uid = ur.uid
INNER JOIN `role` AS `r` ON r.rid = ur.rid
INNER JOIN `field_data_field_first_name` AS `f` ON f.entity_id = ur.uid
INNER JOIN `field_data_field_last_name` AS `l` ON l.entity_id = ur.uid
WHERE 'role.name' = 'admin'
What can i do to get what i want.
Change it to:
WHERE r.name = 'admin'
The way you wrote it, you were comparing two literal strings, not comparing a column to a string.
Also, you have to use r.name rather than role.name -- once you assign an alias to a table, you can't refer to the original table name.

SQL UPDATE with INNER JOIN

I'm trying to use this query
UPDATE products
SET products.product_price = '87.00000'
FROM products
INNER JOIN product_category
ON products.product_id = product_category.product_id
WHERE product_category.category_id = '64'
However I receive this 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 'FROM table products INNER JOIN table product_category ON prod' at line 3
I don't see any syntax error. I have made this query from examples on this forum.
remove the FROM products line.
and put the
SET ... line just before the WHERE clause.
to be clear :
UPDATE ...
JOIN ...
SET ...
WHERE ...
you could also do
UPDATE products p
SET p.product_price='87.00000'
WHERE EXISTS (SELECT NULL
FROM product_category pc
WHERE p.product_id = pc.product_id
AND pc.category_id = '64');
You can use the following:
UPDATE products
INNER JOIN product_category
ON products.product_id = product_category.product_id
SET products.product_price = '87.00000'
WHERE product_category.category_id = '64';
See SQL Fiddle with Demo
UPDATE products
INNER JOIN product_category
ON products.product_id = product_category.product_id
SET products.product_price = '87.00000'
WHERE product_category.category_id = '64'
I think the SET clause needs to be after the table references and before the WHERE clause, like this:
UPDATE products
INNER
JOIN product_category
ON products.product_id = product_category.product_id
SET products.product_price = '87.00000'
WHERE product_category.category_id = '64'
Here's how I get that syntax when I need an UPDATE like that. I always start with a select statement, and that lets me know which rows are going to be updated. For example:
SELECT p.*, c.*
FROM products p
JOIN product_category c
ON p.product_id = c.product_id
WHERE c.category_id = '64'
To convert that to an update statement, I add a "SET" clause after the table references and before the WHERE clause, and then replace "SELECT ... FROM" with the "UPDATE" keyword. Voila.