SELECT h.hacker_id
, FIRST(h.name)
FROM Hackers h
JOIN Challenges c
ON h.hacker_id = c.hacker_id
JOIN Difficulty d
ON c.difficulty_level = d.difficulty_level
JOIN Submissions s
ON h.hacker_id = s.hacker_id
AND c.challenge_id = s.challenge_id
GROUP
BY h.hacker_id
HAVING SUM(s.score = d.score) > 1
ORDER
BY SUM(s.score = d.score) DESC
, h.hacker_id
I'm trying to join a couple of tables and I think the logic is pretty straightforward. However,I got an 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 '(h.name)". Any help would be appreciated!
Related
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'
I'm trying to solve this challenge by using Subquery, I don't know what are the syntax wrong in my code.
select h.hacker_id, h.name from hackers
join (
select s.submission_id, s.hacker_id, s.score, d.score
from submissions s
join challenges c on c.challenge_id = s.challenge_id and
c.hacker_id = s.hacker_id
join diffculty d on c.difficulty_level = d.difficulty_level
where (s.score = d.score)
group by s.submission_id
) as Result(SubId, HID, D1, D2)
on h.hacker_id = Result.HID
having count(Result.SubId) > 1
order by count(Result.SubId) desc, h.name;
Error:
ERROR 1064 (42000) at line 1: 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 '(SubId, HID, D1, D2)
on h.hacker_id = Result.HID
having count(R' at line 12
this is the link of challenge: https://www.hackerrank.com/challenges/full-score/problem
You can to (re)name the columns in the table expression itself as in:
select h.hacker_id, h.name
from hackers
join ( -- here you started a "table expression"
select s.submission_id as SubId, -- renamed here
s.hacker_id as HID, -- renamed here
s.score as D1, -- renamed here
d.score as D2 -- renamed here
from submissions s
join challenges c on c.challenge_id = s.challenge_id and
c.hacker_id = s.hacker_id
join diffculty d on c.difficulty_level = d.difficulty_level
where (s.score = d.score)
group by s.submission_id
) as Result on h.hacker_id = Result.HID
group by h.hacker_id -- you need to group if you want to use HAVING
having count(Result.SubId) > 1
order by count(Result.SubId) desc, h.name;
I am testing one of my project it's opencart
Basically i wanna know my table name for sure for example oc20_product and oc20_manufacturer are table names aren't them ? and this the others actual columns ?
What is my table and column name for sure ?
Thanks in advance
Notice:
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 '%' AND p.status = 1 GROUP BY m.manufacturer_id ORDER BY m.name
ASC' at line 4 Error No: 1064
SELECT m.manufacturer_id, m.name, COUNT(p.product_id) AS products_total
FROM oc20_product p
LEFT JOIN oc20_product_description pd
ON pd.product_id = p.product_id
LEFT JOIN oc20_manufacturer m
ON m.manufacturer_id = p.manufacturer_id
WHERE pd.name LIKE '%mycard'%' AND p.status = 1
GROUP BY m.manufacturer_id
ORDER BY m.name ASC in
wrong quote in like pattern you could use a concat for an easy pattern format
SELECT m.manufacturer_id
, m.name
, COUNT(p.product_id) AS products_total
FROM oc20_product p
LEFT JOIN oc20_product_description pd ON pd.product_id = p.product_id
LEFT JOIN oc20_manufacturer m ON m.manufacturer_id = p.manufacturer_id
WHERE pd.name LIKE concat('%',mycard,'%') AND p.status = 1
GROUP BY m.manufacturer_id, m.name
ORDER BY m.name ASC
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 have three tables A B C and i'm trying to retrieve information from all three.
A has the columnns userid avatar username and B has the column postid, dateshared and C has the column commenter postid datecommented.
I'm trying to run the query
Select C.comment, C.commenter, C.datecommented, B.postid, B.dateshared A.username A.avatar from B Left Join C Left join A on C.postid = B.postid AND A.userid = C.commenter where B.postid IN ('1','2','3') order by C.dateshared desc
but it gives 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 'where B.postid IN ('1', '2', '3') order by C.dateshared '
Can anyone point out what I'm doing wrong or suggest how to go about it?
Each LEFT JOIN requires its own ON condition:
SELECT C.comment, C.commenter, C.datecommented, B.postid, B.dateshared, A.username A.avatar
FROM B
LEFT JOIN
C
ON C.postid = B.postid
LEFT JOIN
A
ON A.userid = C.commenter
WHERE B.postid IN ('1','2','3')
ORDER BY
C.dateshared desc
This should work for you, your query had some syntax errors:
Select C.comment,C.commenter,C.datecommented,B.postid,B.dateshared,A.username,A.avatar
from B
Left Join C on C.postid = B.postid
Left join A on A.userid = C.commenter
where B.postid IN ('1','2','3')
order by C.dateshared desc