Why is MySQL statement ignored? [duplicate] - php

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

Related

How can I get data from 3 tables?

I have 3 tables: 'news', 'comments' and 'rub'. I need to get all data from 'news', get 'title' from 'rub' and count the number of comments for every news.
Now I'm not able to get 'title' from 'rub'. The code below works:
SELECT n.id,n.header,n.text, COUNT(c.news_id) AS comments
FROM news n
LEFT JOIN comments c ON n.id = c.news_id
GROUP BY n.id
ORDER by n.id
LIMIT 30
But I need something like that and this code doesn't work (Call to a member function fetch_array() on a non-object):
SELECT n.id,n.header,n.text,r.title COUNT(c.news_id) AS comments
FROM news n,rub r
LEFT JOIN comments c ON n.id = c.new_id AND r.news_id=n.id
GROUP BY n.id ORDER by n.id
LIMIT 30
How to fix it?
It's not really clear to me what you are asking, but you need another JOIN. And you need to move the join condition for the rub table to that JOIN keyword, not into the join condition for the comments table
SELECT n.id,
n.header,
n.text,
r.title, -- comma was missing (as halfer mentioned)
COUNT(c.news_id) AS comments
FROM news n
JOIN rub r ON r.news_id=n.id --- this is missing
LEFT JOIN comments c ON n.id = c.new_id -- no join condition for rub/news here
GROUP BY n.id
ORDER by n.id
LIMIT 30
Your usage of the GROUP BY operator is also wrong. The above query will (rightfully) be rejected by all other DBMS.
Please read the following to understand why your GROUP BY is wrong:
http://www.mysqlperformanceblog.com/2006/09/06/wrong-group-by-makes-your-queries-fragile/
http://rpbouman.blogspot.de/2007/05/debunking-group-by-myths.html

GROUP_CONCAT too slow

Hello I added GROUP_CONCAT function to my query and that function killed my query :/.
My query is :
SELECT u.username,a.user_id,a.id,a.text,a.lang as fromLang,b.lang as toLang,GROUP_CONCAT(DISTINCT b.id) AS translation_ids FROM sentence as a
INNER JOIN sentence_relationship as sr ON
(sr.sentence_id = a.id)
INNER JOIN sentence as b ON
(b.id = sr.translation_id AND a.id = sr.sentence_id)
INNER JOIN users as u ON
(u.id = a.user_id) GROUP BY a.id LIMIT 10;
What is wrong with that query ?
This is your query (somewhat formatted):
SELECT u.username, a.user_id, a.id,a.text,a.lang as fromLang, b.lang as toLang,
GROUP_CONCAT(DISTINCT b.id) AS translation_ids
FROM sentence a INNER JOIN
sentence_relationship sr
ON sr.sentence_id = a.id INNER JOIN
sentence b
ON b.id = sr.translation_id AND a.id = sr.sentence_id INNER JOIN
users as u
ON u.id = a.user_id
GROUP BY a.id
LIMIT 10;
It is unclear from your question whether the group_concat() was added with the group by. That could slow things down.
The limit 10 is taking the first 10 a.ids that match (the group by does an implicit ordering). If you do this with a subquery, it will probably speed up the query:
SELECT u.username, a.user_id, a.id,a.text,a.lang as fromLang, b.lang as toLang,
GROUP_CONCAT(DISTINCT b.id) AS translation_ids
FROM (select s.*
from sentence s
order by a.id
limit 10
) a INNER JOIN
sentence_relationship sr
ON sr.sentence_id = a.id INNER JOIN
sentence b
ON b.id = sr.translation_id AND a.id = sr.sentence_id INNER JOIN
users as u
ON u.id = a.user_id
GROUP BY a.id;
This assumes that all the joins do work and match records. If the joins are used for filtering, then you may get fewer than 10 rows back.

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

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

How to write this mysql 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

Categories