This question already has answers here:
Alternative to Intersect in MySQL
(9 answers)
Closed 8 years ago.
Trying to see what video categories "me_id" and "you_id" have both watched with:
SELECT c.title, COUNT(*) AS popularity
FROM video v
JOIN user u ON v.user_id = u.id
JOIN v_cat vc ON c.id = vc.vid_id
JOIN cat c ON c.id = vc.cat_id
JOIN u_cat uc ON uc.cat_id = c.id
WHERE uc.user_id = '$me_id'
INTSERSECT
SELECT c.title, COUNT(*) AS popularity
FROM video v
JOIN user u ON v.user_id = u.id
JOIN v_cat vc ON c.id = vc.vid_id
JOIN cat c ON c.id = vc.cat_id
JOIN u_cat uc ON uc.cat_id = c.id
WHERE uc.user_id = '$you_id'
GROUP BY c.title
ORDER BY uc.id DESC LIMIT 0, 10
I am working with PHP/MYSQL any thoughts?
MySQL doesn't have INTERSECT.
JOIN JOIN , Is that valid syntax?
FROM video v JOIN JOIN v_cat vc ON c.id = vc.vid_id
in the above line u have to use join one time..
I see two consecutive JOIN keywords in your first select but without any error message this is going to be difficult to debug.
Instead of INTERSECT, you can use the key word UNION or UNION ALL, this basically will do the intersection select. See http://dev.mysql.com/doc/refman/5.0/en/union.html
Related
Hello i'm learning sql and i have some issues with joins(which i have problems understanding them)
I have this issue
#1066 - Not unique table/alias: 'tbl_respuestas'
what the query supposed to do, is count how many people(general,ignore user) has answer 'x', in 'y' question of 'z' survey
SELECT COUNT(*) FROM tbl_respuestas
INNER JOIN tbl_encuesta_usuario ON tbl_encuesta_usuario.user_id = user.id
INNER JOIN tbl_encuesta ON tbl_encuesta.id = tbl_encuesta_usuario.tbl_encuesta_id
INNER JOIN tbl_encuesta_has_tbl_preguntas ON tbl_encuesta_has_tbl_preguntas.tbl_encuesta_id = tbl_encuesta.id
INNER JOIN tbl_preguntas ON tbl_preguntas.id = tbl_encuesta_has_tbl_preguntas.tbl_preguntas_id
INNER JOIN tbl_preguntas_has_tbl_respuestas ON tbl_preguntas_has_tbl_respuestas.tbl_preguntas_id = tbl_preguntas.id
INNER JOIN tbl_respuestas ON tbl_respuestas.id = tbl_preguntas_has_tbl_respuestas.tbl_respuestas_id
WHERE tbl_respuestas.respuesta = 2
line SELECT COUNT(*) FROM tbl_respuestas
and line INNER JOIN tbl_respuestas
does not makes sense, hence the error.
Unless it is what you want then you need to give then different name/alias like below:
SELECT COUNT(*) FROM tbl_respuestas r
INNER JOIN tbl_respuestas r2
Also as a quick note you can rewrite the entire sql like below.
It is good practice to give your tables a name for shorter referencing and makes the sql look a little cleaner.
Also if both tables you are trying to join has the same column name then you can use the keyword USING instead of having to write that long line tbl_encuesta_usuario.user_id = user.id
Please be sure to put r and r2 in its prope place
SELECT COUNT(*) FROM tbl_respuestas r
INNER JOIN tbl_encuesta_usuario u USING user_id
INNER JOIN tbl_encuesta e ON e.id = u.tbl_encuesta_id
INNER JOIN tbl_encuesta_has_tbl_preguntas hp ON hp.tbl_encuesta_id = e.id
INNER JOIN tbl_preguntas p ON p.id = hp.tbl_preguntas_id
INNER JOIN tbl_preguntas_has_tbl_respuestas hr ON hr.tbl_preguntas_id = p.id
INNER JOIN tbl_respuestas r2 ON r2.id = hr.tbl_respuestas_id
WHERE r.respuesta = 2
This question already has answers here:
SQL JOIN and different types of JOINs
(6 answers)
Closed 9 years ago.
Why is n.author='$host[id]' ignored?
$host[id]=5;
SELECT
n.id,n.name,n.text,
r.title,
COUNT(c.news_id) comments
FROM news n LEFT JOIN rub r
ON
r.news_id=n.id
LEFT JOIN comments c
ON
n.id = c.news_id AND c.status='1' AND n.author='$host[id]'
GROUP BY n.id
ORDER BY n.id DESC LIMIT 10
I need only those rows where 'news'.'author'=$host[id], but executing this query I got all the news from the table. Why does it happen?
$host[id]=5;
SELECT n.id,n.name,n.text, r.title, COUNT(c.news_id) comments
FROM news n
LEFT JOIN rub r ON r.news_id = n.id
LEFT JOIN comments c ON c.news_id = n.id AND c.status = '1'
WHERE n.author = '$host[id]'
GROUP BY n.id
ORDER BY n.id DESC LIMIT 10
$host['id']=5;
SELECT
n.id,n.name,n.text,
r.title,
COUNT(c.news_id) comments
FROM news n LEFT JOIN rub r
ON
r.news_id=n.id
LEFT JOIN comments c
ON
n.id = c.news_id AND c.status='1' AND n.author="'{$host['id']}'";
GROUP BY n.id
In Question and Answer system I am trying to get comment for question and answer and the title of that comment.
I have done so far and getting comment's content but now the issue is if the comment is on the answer it is not getting the question title. So how can I write query so it will get all comment from question and answer both and will get the title of the question.
Here I have done so far.
SELECT c.postid, p.title, c.type, c.userid, c.content, c.parentid, u.handle, u.email, u.avatarblobid, u.avatarwidth, u.avatarheight FROM qa_posts p
JOIN qa_posts c ON (p.postid = c.parentid)
LEFT JOIN qa_users u ON c.userid = u.userid
WHERE c.type = 'C'
AND p.flagcount = 0
ORDER BY c.postid DESC
LIMIT 5
Here is my table image and you can find type column is for Q, C and A question, comment and answer respectively.
Thanks a lot
EDIT:-------------------------------------------------------------
I have tried to check conditionally but this code also not working.. I hope you expert will guide to to correct this code
JOIN qa_posts AS parentposts
ON qa_posts.postid=(
CASE
LEFT(perentposts.type, 1)
WHEN
'A'
THEN
parentposts.parentid
ELSE
parentposts.postid
END
)
JOIN qa_posts AS cposts
ON parentposts.postid=cposts.parentid
LEFT JOIN qa_users AS cusers
ON cposts.userid=cusers.userid
JOIN (SELECT postid FROM qa_posts
WHERE type=$
ORDER BY qa_posts.created DESC
LIMIT 5)
y ON cposts.postid=y.postid
WHERE qa_posts.type='Q'
AND ((parentposts.type='Q') OR (parentposts.type='A'))
try this
SELECT * FROM qa_posts p
LEFT JOIN qa_users u ON c.userid = u.userid
WHERE p.type = 'C'
AND p.flagcount = 0
ORDER BY p.postid DESC
LIMIT 5
i have a problem.
I'm running following query. I have only one record in my database but i'm getting 9 results.
SELECT c.id, c.rk
FROM cv AS c, employee AS e , cvCat AS cv_cat
WHERE c.status=1
AND c.empIDFK = e.id
AND cv_cat.categoryFK IN ( 17,18,19,38,39,40,41,44,45,46 )
AND cv_cat.cvFK = c.id
Can someone please let me know if they is any problem with this query. Why i'm getting 9 results rather then just 1 result.
This query should only display one record but its showing 9 results.
When you do
FROM cv AS c, employee AS e , cvCat AS cv_cat
You are doing an implicit join of the three tables. If you want to get distinct records you can add DISTINCT after your select:
SELECT DISTINCT c.id, c.rk
FROM cv AS c, employee AS e , cvCat AS cv_cat
WHERE c.status=1
AND c.empIDFK = e.id
AND cv_cat.categoryFK IN ( 17,18,19,38,39,40,41,44,45,46 )
AND cv_cat.cvFK = c.id
Use explicit joins:
SELECT c.id, c.rk
FROM cv c
INNER JOIN employee e ON e.id = c.empIDFK
INNER JOIN cvCat cv_cat ON cv_cat.cvFK = c.id
WHERE c.status = 1
AND cv_cat.categoryFK IN (17,18,19,38,39,40,41,44,45,46)
I think the 9 result row "issue" is based on your JOIN syntax, as that kind of implicit join will return all rows (of each table) as a joined result, since you are only pulling out c.id and c.rk it may look like MySQL sent the same result back 9 times.
Side Note: Implicit joins are being deprecated, using explicit joins, such as...
SELECT c.id, c.rk
FROM cv c
LEFT JOIN employee e ON c.empIDFK = e.id
LEFT JOIN cvCat cv_cat ON c.id = cv_cat.cvFK
WHERE...
Will help "future-proof" your code a bit and add a little more self-describing syntax to the whole query.
I have three tables tb_poll,tb_poll_answer,tb_poll_votes
tb_poll has id,question
tb_poll_answer has id,poll_id,answer
tb_poll_votes has id,poll_answer_id
I want to select all questions and it answers(with count votes).
Finally i want to list
Id Question Answers
1 Which is....? 1.PHP(70) 2.ASP(30) 3.JSP(2)
2 ...... .......
How to write best mysql query here?
Try this query now:
SELECT q.id,q.question,
GROUP_CONCAT(CONCAT(a.id,'.',a.answer,COUNT(v.id) SEPARATOR ' ')) AS `Answers`
FROM `tb_poll` AS q
LEFT JOIN `tb_poll_answers` AS a
ON q.id = a.poll_id
LEFT JOIN `tb_poll_votes` AS v
ON a.id = v.poll_answer_id
WHERE 1
GROUP BY q.id
ORDER BY q.id
Finally i got result through sub query
SELECT q.id,q.question,av.vt,GROUP_CONCAT(CONCAT(av.answer,av.vt) SEPARATOR ' ')
AS Answers FROM tb_poll AS q LEFT JOIN
(SELECT a.answer,a.poll_id,COUNT(v.id) AS vt FROM tb_poll_answers AS a LEFT JOIN tb_poll_votes AS v ON a.id=v.poll_answer_id GROUP BY a.id) AS av
ON q.id=av.poll_id
GROUP BY q.id