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 ;
Related
i want to get some details not in ratings table with user name and i made a cross join query. but it shows me syntax error
SELECT applications.reference as "User id", applications.id
FROM applications
cross join ratings where (applications.id) NOT IN ( SELECT ratings.application_id FROM ratings )
INNER JOIN ratings ON users.id=ratings.user_id
error shows
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INNER JOIN ratings ON users.id=ratings.user_id LIMIT 0, 25' at line 2
i want to get user details in applications where not in ratings table with user name in users table. how can i fix this?
I think you need such a query :
SELECT a.reference as "User id", a.id
FROM applications a
CROSS JOIN ratings r
INNER JOIN users u ON u.id = r.user_id
WHERE a.id != r.application_id;
SELECT applications.reference as "User id", applications.id
FROM applications
INNER JOIN ratings ON users.id=ratings.user_id
cross join ratings where (applications.id) NOT IN ( SELECT ratings.application_id FROM ratings )
I have one problem with my SELECT query in my blog page.
I want comment count of each blog when comment status=1.
I am apply following query..
SELECT CONCAT(u.first_name," ",u.last_name) name, r.*,
IF(c.status=1,COUNT(c.id)) as comment
FROM users u
RIGHT JOIN resources r ON u.id = r.created_by
LEFT JOIN comments c ON r.id = c.resource_id
WHERE r.type = 1
AND r.status=1
GROUP BY r.id
ORDER BY r.created_date DESC
LIMIT 0,5
but it giving SYNTAX ERROR..
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your
SQL syntax; check the manual that corresponds to your MariaDB server version for the right
syntax to use near ') as comment FROM users u RIGHT JOIN resources r ON u.id = r.created_by
LEFT JOI' at line 1
Please tell me where I am wrong.
Thanks
If statement contains three expressions. First, the expression, second the value returned if condition is true and third if condition is false so you are missing the third expression.
Try the below code
SELECT CONCAT(u.first_name," ",u.last_name) name,r.*,IF(c.status=1,COUNT(c.id), 0) as comment
FROM users u RIGHT JOIN resources r ON u.id = r.created_by
LEFT JOIN comments c ON r.id = c.resource_id
WHERE r.type = 1
AND r.status=1
GROUP BY r.id
ORDER BY r.created_date DESC
LIMIT 0,5
Select concat(u.first_name," ",u.last_name) name,r.*,
case when c.status=1 then COUNT(c.id) end as comment
FROM users u RIGHT JOIN resources r ON u.id = r.created_by
LEFT JOIN comments c ON r.id = c.resource_id
WHERE r.type = 1
AND r.status=1
GROUP BY r.id
ORDER BY r.created_date DESC
LIMIT 0,5
https://www.w3schools.com/sql/func_mysql_case.asp
if function requires 3 parameters to be passed to it. IF(expression ,expr_true, expr_false) is how it should be used.
Have a look at https://www.w3resource.com/mysql/control-flow-functions/if-function.php
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
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
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.