MySQL Query Not Working Properly - php

Where is the mistake?
Thank you!
SELECT Table.id,
Table.name,
Table.kommentar,
Table.pictureurl
WHERE Table.news_id = 5, //without this line the query works!
COUNT( Table1.comment_id ) AS numComments
FROM DATABASE.news_comments AS Table
LEFT JOIN DATABASE.news_comments_comments AS Table1
ON Table1.comment_id = Table.id
GROUP BY Table.id
ORDER BY Table.id DESC LIMIT 0,50

WHERE clause should be after the FROM clause
SELECT `Table`.id,
`Table`.name,
`Table`.kommentar,
`Table`.pictureurl,
COUNT( Table1.comment_id ) AS numComments
FROM DATABASE.news_comments AS `Table`
LEFT JOIN DATABASE.news_comments_comments AS Table1
ON Table1.comment_id = `Table`.id
WHERE `Table`.news_id = 5 // <=== HERE
GROUP BY `Table`.id
ORDER BY `Table`.id DESC LIMIT 0,50
One more thing, your alias which is Table should be escape with backtick since it a Reserved Keyword in MySQL

SELECT Table.id,
Table.name,
Table.kommentar,
Table.pictureurl,
COUNT( Table1.comment_id ) AS numComments
FROM DATABASE.news_comments AS Table
LEFT JOIN DATABASE.news_comments_comments AS Table1
ON Table1.comment_id = Table.id
WHERE Table.news_id = 5 //===> where should be here
GROUP BY Table.id
ORDER BY Table.id DESC LIMIT 0,50

Related

MySQL Query - alternative

My query is too long (3-4s). Any idea's how make this faster?
SELECT u.id AS id_uzytkownika,
u.login,
u.ranga,
u.online_light AS online,
(SELECT MAX(id)
FROM uzytkownicy_zdjecia
WHERE id_uzytkownika = u.id
AND prywatna =0) AS id_fotki,
(SELECT fotka
FROM uzytkownicy_zdjecia
WHERE id = id_fotki) AS fotka ,
(SELECT srednia_ocen
FROM uzytkownicy_zdjecia
WHERE id = id_fotki) AS srednia_ocen,
(SELECT ile_ocen
FROM uzytkownicy_zdjecia
WHERE id = id_fotki) AS ile_ocen
FROM uzytkownicy u
WHERE u.foto =1
AND u.plec = "mezczyzna"
ORDER BY srednia_ocen DESC,
ile_ocen DESC,
id_fotki DESC LIMIT 42
Could you maybe explain your table structure and what this query is about? Since the column names are not in English, most readers will probably have a problem understanding what you are trying to do here...
In general, it looks like you have a LOT of nested SELECTS here to the same table - is there any special reason for that?
Try this:
SELECT u.id AS id_uzytkownika,
u.login,
u.ranga,
u.online_light AS online,
(SELECT MAX(id)
FROM uzytkownicy_zdjecia
WHERE id_uzytkownika = u.id
AND prywatna =0) AS id_fotki,
z.fotka,
z.srednia_ocen,
z.ile_ocen
FROM uzytkownicy u
JOIN uzytkownicy_zdjecia z
ON u.id_fotki = z.id
WHERE u.foto =1
AND u.plec = "mezczyzna"
ORDER BY srednia_ocen DESC,
ile_ocen DESC,
id_fotki DESC LIMIT 42
I've substituted 3 subqueries with a JOIN. The first subquery has a different condition, if you can merge that condition withn others queries you can remove that.
Pay attention You have named id_fokta the result of subquery, but the same name has the primary key of uzytkownicy table
You don't have to query the same table with the same criteria again and again. Replace your sub-selects with a simple join. A LEFT JOIN, if it is possible that no matching record exists.
SELECT
u.id AS id_uzytkownika,
u.login,
u.ranga,
u.online_light AS online,
(
SELECT MAX(id)
FROM uzytkownicy_zdjecia
WHERE id_uzytkownika = u.id
AND prywatna =0
) AS id_fotki,
uz.fotka,
uz.srednia_ocen,
uz.ile_ocen
FROM uzytkownicy u
LEFT JOIN uzytkownicy_zdjecia uz ON uz.id = u.id_fotki
WHERE u.foto = 1
AND u.plec = "mezczyzna"
ORDER BY u.srednia_ocen DESC,
u.ile_ocen DESC,
u.id_fotki DESC LIMIT 42
An alternative way to write the query above is to aggregate first and then join:
SELECT
u.id AS id_uzytkownika,
u.login,
u.ranga,
u.online_light AS online,
uzz.id_fotki,
uz.fotka,
uz.srednia_ocen,
uz.ile_ocen
FROM uzytkownicy u
LEFT JOIN uzytkownicy_zdjecia uz ON uz.id = u.id_fotki
LEFT JOIN
(
SELECT id_uzytkownika, MAX(id) AS id_fotki
FROM uzytkownicy_zdjecia
WHERE prywatna =0
GROUP BY id_uzytkownika
) uzz ON uzz.id_uzytkownika = u.id
WHERE u.foto = 1
AND u.plec = "mezczyzna"
ORDER BY u.srednia_ocen DESC,
u.ile_ocen DESC,
u.id_fotki DESC LIMIT 42
By the way: What is "mezczyzna"? A string? Then this should be single quotes.

MySQL left join count with condition

I have two tables:
cc_videos with fields: id, challenge_id, upload_date, owner_id
cc_video_votes with fields id, video_id, vote_date
Field "id" in "cc_videos" corresponds to a field "video_id" in "cc_video_votes" and I have a statement like this:
SELECT cc_videos.*, COUNT(video_id) AS votes_count
FROM cc_videos LEFT JOIN cc_video_votes
ON cc_videos.id = cc_video_votes.video_id
GROUP BY cc_videos.id
ORDER BY votes_count desc
Now this works OK - lists ALL VIDEOS sorted by videos with highest vote rate first. Now i want to put condition to list only videos from "cc_videos" WHERE challenge_id matches variable "$challenge_id", but when I put condition like this it returns 0 results:
SELECT cc_videos.*, COUNT(video_id) AS votes_count
FROM cc_videos WHERE challenge_id = "$challenge_id"
LEFT JOIN cc_video_votes
ON cc_videos.id = cc_video_votes.video_id
GROUP BY cc_videos.id
ORDER BY votes_count desc
Am I using WHERE clause correctly ?
No you are not.
SELECT cc_videos.*, COUNT(video_id) AS votes_count
FROM cc_videos
LEFT JOIN cc_video_votes
ON cc_videos.id = cc_video_votes.video_id
WHERE cc_videos.challenge_id = "$challenge_id"
GROUP BY cc_videos.id
ORDER BY votes_count desc
WHERE syntax should stay in clauses, check documentation here
Change your query to:
SELECT cc_videos.*,
Count(video_id) AS votes_count
FROM cc_videos
LEFT JOIN cc_video_votes
ON cc_videos.id = cc_video_votes.video_id
WHERE cc_videos.challenge_id = "$challenge_id"
GROUP BY cc_videos.id
ORDER BY votes_count DESC

sql syntax error in a subquery selection for a random choice

select op.id, op.nome,op.cognome,
(select tp.inizio
from turni_preconf as tp
where tp.tot_ore = op.ore_giornaliere
order by rand()
limit 1
) as randomin,
addtime(randomin,op.ore_giornaliere_time) as rnout
from operatori as op
the error are : Unknown column 'randomin' in 'field list' i have a little doubt for the sub query ex select in but i have tryed and don't work
Move the subquery to the FROM clause...
select op.id, op.nome,op.cognome,
r.val as randomin,
addtime(r.val, op.ore_giornaliere_time) as rnout
from operatori as op,
(select tp.inizio as val
from turni_preconf as tp, operatori as op2
where tp.tot_ore = op2.ore_giornaliere
order by rand()
limit 1
) as r
You can't use field aliases in the same "level" of the query, e.g.
select somefield AS foo, otherfield = foo AS bar
^^^^^^--create alias
^^^^--use alias, not ok
UNTESTED:
Updated join was wrong I didn't have the right criteria.
Perhaps...
select op.id, op.nome,op.cognome, addtime(randomin,op.ore_giornaliere_time) as rnout,
b.randomin
from operatori as op
INNER JOIN
(select tot_ore, tp.inizio as randomin
from turni_preconf as tp
order by rand()
limit 1
) b on b.tot_ore = op.ore_giornaliere

SQL query not returning count value

SQl query running to return the total count from the query.
this code works when running SQL with PHPmyAdmin
But on the page it is not displaying echo of the count ?
Not sure if I could have overlooked something here.
Many Thanks!
$sql2=mysql_query("SELECT count(*)
FROM main_table LEFT JOIN houses ON main_table.housenumber = houses.housenumber AND main_table.streetname = houses.streetname
WHERE main_table.city='1'
group by main_table.city ORDER BY average DESC, houseID DESC, reviewID DESC;");
while($row=mysql_fetch_array($sql2))
{
$count=$row['count'];
echo $count;;
}
Try this ....
$sql2=mysql_query("SELECT
COUNT(*) AS count
FROM
main_table
LEFT JOIN houses
ON main_table.housenumber = houses.housenumber
AND main_table.streetname = houses.streetname
WHERE main_table.city = '1'
GROUP BY main_table.city
ORDER BY average DESC,
houseID DESC,
reviewID DESC") ;
while($row=mysql_fetch_array($sql2))
{
$count=$row['count'];
echo $count;
}
You have mistake in your query, you are not adding count in select as aliases, and below in while you are using aliases . Try this.
name your column :
...mysql_query("SELECT count(*) as count....
add SELECT count(*) as 'count' from ...
try starting your query with :
SELECT count(*) as count ...

SQL query GROUP BY id filter?

THIS IS THE OUTPUT OF THE QUERY:
SELECT * FROM
((SELECT
privatemsgs.id,
privatemsgs.useradn,
privatemsgs.useraid,
privatemsgs.title,
privatemsgs.created,
privatemsgs.timee,
privatemsgs.isread,
u.photo AS creatorphoto,
privatemsgs.relatedto
FROM privatemsgs
LEFT JOIN
users AS u ON(privatemsgs.useraid = u.id)
WHERE userbid='$myid'
AND relatedto=0 and bdel=1)
UNION
(SELECT
privatemsgs.id,
privatemsgs.useradn,
privatemsgs.useraid,
privatemsgs.title,
privatemsgs.created,
privatemsgs.timee,
privatemsgs.isread,
u.photo AS creatorphoto,
rel.relatedto
FROM privatemsgs AS rel
JOIN privatemsgs ON(rel.relatedto = privatemsgs.id)
LEFT JOIN
users AS u ON(rel.useraid = u.id)
WHERE rel.userbid='$myid'
)) privatemsgs
GROUP BY id
ORDER BY timee DESC
I got double id "2". first 1 with "isread = 0", second with "isread = 1".
When I added "group by id", I've got (line 2)
But i need the output to show that isread = 1 (like line 3)
How do i fix it?
add MAX(isread) near the *. GROUP BY works only on Aggregate functions

Categories