How to write this mysql query? - php

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

Related

Why is MySQL statement ignored? [duplicate]

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

MySQL: How to get comment content and the post title

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

How to get individual count of same value rows with join

I just want to know how to get individual count of same value rows with join using mysql and help are definitely appreciated
Here is my mysql Query
SELECT c.`id`
FROM categories c inner join subcategories sc
on c.`id` = sc.`cat_id`
See the Response screeshot
try this query:
SELECT c.`id`, count(1)
FROM categories c inner join subcategories sc
on c.`id` = sc.`cat_id`
GROUP BY c.`id`
It's quite a basic task. Consider studying GROUP BY and aggregate functions.
SELECT id, COUNT(*)
FROM categories c
INNER JOIN subcategories sn ON c.id = sn.cat_id
GROUP BY c.id

Why is this SQL giving a syntax error? [duplicate]

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

Is there an alternative for MYSQL HAVING

I'm trying to find some records based on the tags they have. In order to find out which tags a record has i add them to the result using a subquery. To find out which results should be returned i added a having statement on the end of the query. But something tells me this is not the best way.
SELECT e.id, e.title, e.text, e.introduction,
UNIX_TIMESTAMP(e.starts_on) AS starts_on,
UNIX_TIMESTAMP(e.ends_on) AS ends_on,
m.id AS meta_id,
m.url,
cm.title AS category_title,
cm.url AS category_url,
CONCAT(
",",
(
SELECT GROUP_CONCAT(t.id)
FROM modules_tags AS mt
JOIN tags AS t ON t.id = mt.tag_id
WHERE mt.other_id = e.id
),","
) AS tags_search
FROM event_posts AS e
INNER JOIN meta AS m ON e.meta_id = m.id
LEFT JOIN event_categories AS c ON e.category_id = c.id
LEFT JOIN meta AS cm ON c.meta_id = cm.id
LEFT JOIN modules_tags AS mt ON mt.other_id = e.id
LEFT JOIN tags AS t ON t.id = mt.tag_id
WHERE 1 HAVING tags_search LIKE '%,5,%' AND tags_search LIKE '%,6,%'
FIND_IN_SET() is the best way to find values in comma separated values in MySQL.

Categories