No results showing up AND error as well with where clause - mysql

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.

Related

Error in JOIN while using Common Table Expresion

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'

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 ;

SELECT query IF CONDITION

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

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.

phpmyadmin update query using 3 tables in giving error

I am getting the following Error
064 - 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 catalog_product_flat_1 a inner join
catalog_product_entity c on a.sku' at line 3
when I try out the below query on phpmyadmin
update a
set a.small_image = b.value
from `catalog_product_flat_1` a
inner join `catalog_product_entity` c
on a.sku = c.sku
inner join `catalog_product_entity_media_gallery` b
on b.entity_id = c.entity_id
I also tryout without using alias but still same issue
The SET clause comes after the JOIN clauses:
UPDATE `catalog_product_flat_1` a
inner join `catalog_product_entity` c
on a.sku = c.sku
inner join `catalog_product_entity_media_gallery` b
on b.entity_id = c.entity_id
SET a.small_image = b.value
See the documenation:
http://dev.mysql.com/doc/refman/5.5/en/update.html