Help with a joined query (MySQL) - php

Hello can anybody see why this query fails?
SELECT A.idAd, A.ads_in_Cat, A.title, A.currency, A.price,
A.in_dpt, A.description, A.featured FROM ads A
LEFT JOIN featured F ON F.ad = A.idAd
INNER JOIN dept D ON D.id_dept = A.in_dpt
INNER JOIN sub_cat_ad S ON S.id_sub_cat = A.ads_in_Cat
INNER JOIN cat_ad C ON C.idCat_ad = S.from_cat_ad
ORDER BY A.featured DESC LIMIT :limit, :offset
But this one works:
SELECT *FROM ads
LEFT JOIN featured ON featured.ad = ads.idAd
INNER JOIN dept ON dept.id_dept = ads.in_dpt
INNER JOIN sub_cat_ad ON id_sub_cat = ads.ads_in_Cat
INNER JOIN cat_ad ON idCat_ad = sub_cat_ad.from_cat_ad
ORDER BY featured DESC LIMIT :limit, :offset
In the first one, I don't want all columns from the table "ads", the query returns only columns till ...FROM ads A.

If you specify to only select fields from A that's what you get: Only fields from A.
If you want fields from other tables too you have to specify them as well.
SELECT
A.idAd, A.ads_in_Cat, A.title, A.currency, A.price, A.in_dpt, A.description, A.featured,
F.*,
D.*,
S.*,
C.*
FROM ads A
LEFT JOIN featured F ON F.ad = A.idAd
INNER JOIN dept D ON D.id_dept = A.in_dpt
INNER JOIN sub_cat_ad S ON S.id_sub_cat = A.ads_in_Cat
INNER JOIN cat_ad C ON C.idCat_ad = S.from_cat_ad
ORDER BY A.featured DESC LIMIT :limit, :offset

Related

LIMIT LEFT join to last updated row from multiple rows

This is my code i am trying to left join the latest team data, not every piece of data. i have tried just using limit 1 but doesnt return anything
ORDER BY updated DESC LIMIT 1
this doesnt work
Any ideas?
$sql = "SELECT
events.id, events.time,events.status, events.home_team,events.away_team,events.league,
ht.id as home_id,ht.name as home_name,at.name as away_name,
statistics.home_goals,statistics.away_goals,statistics.time as game_time,
leagues.id as league_id,leagues.name as league_name,leagues.type as league_type,
country.name as country_name,country.logo,
hts.home_scored, ats.away_scored,
hts.home_conceeded,ats.away_conceeded,
hts.home_win,ats.away_win,
hts.home_15,ats.away_15,
hts.home_25,ats.away_25,
hts.home_btts, ats.away_btts,
hts.home_fts, ats.away_fts,
hts.home_cs, ats.away_cs,
hts.home_corners_for, ats.away_corners_for,
hts.home_corners_against, ats.away_corners_against,
hts.home_cards, ats.away_cards
FROM events
LEFT JOIN teams ht
ON ht.id = events.home_team
LEFT JOIN teams at
ON at.id = events.away_team
LEFT JOIN leagues
ON leagues.id = events.league
LEFT JOIN country
ON country.id=leagues.country
LEFT JOIN ( SELECT team,home_scored,home_conceeded,home_win,home_15,home_25,home_btts,home_fts,home_cs,home_corners_for,home_corners_against,home_cards FROM team_quick_stats ORDER BY updated DESC) hts
ON ht.id=hts.team
LEFT JOIN ( SELECT team,away_scored,away_conceeded,away_win,away_15,away_25,away_btts,away_fts,away_cs,away_corners_for,away_corners_against,away_cards FROM team_quick_stats ORDER BY updated DESC) ats
ON at.id=ats.team
LEFT JOIN statistics
ON statistics.event_id=events.id
WHERE (events.time BETWEEN $start AND $end) ORDER BY country.list_order, leagues.country ASC , leagues.id ASC, events.time ASC, home_name ASC";
Here's one way. Replace LEFT JOIN (SELECT team... etc....) ats with...
LEFT
JOIN
( SELECT x.team
, x.etc...
FROM team_quick_stats x
JOIN
( SELECT team
, MAX(updated) updated
FROM team_quick_stats
GROUP
BY team
) y
ON y.team = x.team
AND y.updated = x.updated
) ats...

my query returns duplicates

Hello i have a query that selects products from a database, but for some reason it returns duplicates. The problem is in my joins i guess since in the product table there is no duplicate product. Here is my query:
$stmt=$dbh->prepare("SELECT
p.name,
p.slug,
p.id_product,
p.price,
pig.image,
c1.slug as ssubcat,
c2.slug as subcat,
c3.slug as cat
FROM
tbl_products p
INNER JOIN tbl_products_to_categories ptoc
ON ptoc.id_product = p.id_product
INNER JOIN tbl_catalog_categories c1
ON ptoc.id_category = c1.id_category
LEFT JOIN tbl_catalog_categories c2
ON c1.id_parent = c2.id_category
LEFT JOIN tbl_catalog_categories c3
ON c2.id_parent = c3.id_category
INNER JOIN tbl_products_images_gallery pig
ON pig.id_product = p.id_product
WHERE (c1.slug = :slug OR c2.slug = :slug OR c3.slug = :slug )
AND p.active = 1
AND p.quantity = 1
ORDER BY p.name ASC
LIMIT $start, $row_limit");

select count of rows from 2 tables and merge into one row (mysqli)

i create a web app like facebook by php and mysqli
in my app i have a table for posts , one table for likes , and one table for comments
i want to get the number of comments and likes of each post in one row with his post_id!!!
i try some querys likes this :
select `tblpost`.`post_id`, COALESCE(TCOMM.`comment_num`,0) as `c_num`, COALESCE(TLIKE.`like_num`,0) as `l_num`
from
(select `tblpost`.`post_id`, count(*) as `like_num` from `tblpost` join `tbllikes` on `tbllikes`.`post_id` = `tblpost`.`post_id` group by `tblpost`.`post_id`
) TLIKE
inner join
(select `tblpost`.`post_id`, count(*) as `comment_num` from `tblpost` join `tblcomments` on `tblcomments`.`post_id` = `tblpost`.`post_id` group by `tblpost`.`post_id`) TCOMM
on
TCOMM.`post_id` = TLIKE.`post_id`
but i don't know what's my problem
You can do count distincts with two left joins.
Something like this would work if there are fields like_id and comment_id in the tables tbllikes and tblcomments
SELECT
tblpost.post_id AS post_id,
COUNT(DISTINCT tbllikes.like_id) AS likes,
COUNT(DiSTINCT tblcomments.comment_id) AS comments
FROM tblpost
LEFT JOIN tbllikes ON tbllikes.post_id = tblpost.post_id
LEFT JOIN tblcomments on tblcomments.post_id = tblpost.post_id
GROUP BY tblpost.post_id
First, I think you can greatly simplify your query:
select l.post_id,
COALESCE(c.comment_num, 0) as c_num, COALESCE(l.like_num, 0) as l_num
from (select l.post_id, count(*) as like_num
from tbllikes l
group by l.post_id
) l inner join
(select c.post_id, count(*) as comment_num
from tblcomments c
group by c.post_id
) c
on l.post_id = c.post_id;
This will only get you posts that have both likes and comments. To get what you want, use a left join:
select p.post_id,
COALESCE(c.comment_num, 0) as c_num, COALESCE(l.like_num, 0) as l_num
from tblpost p left join
(select l.post_id, count(*) as like_num
from tbllikes l
group by l.post_id
) l
on l.post_id = p.post_id left join
(select c.post_id, count(*) as comment_num
from tblcomments c
group by c.post_id
) c
on c.post_id = p.post_id;

MySQL query to order results by number of votes in another table for the specifc result/row

Okay, so I have a query below I am trying to get to work.
Basically everything works up until the inner join of the 'votes' table. What I am trying to do is order the results of this query in accordance with the number of votes each content row has in another table called votes. I know I'm not too far off from what I need to do!
Thanks in advance!!
mysql_query("
SELECT content.id, content.type, content.title, content.url, users.username
FROM content
INNER JOIN users ON content.uploaderuid = users.id
INNER JOIN votes ON votes.id = content.id
WHERE (content.type = 'pic')
ORDER BY COUNT(votes.id) DESC");
Try doing:
SELECT content.id, content.type, content.title, content.url, users.username
FROM content
INNER JOIN users ON content.uploaderuid = users.id
INNER JOIN (
SELECT id,COUNT(*) as voteCount
FROM votes
GROUP BY id
) v ON v.id = content.id
WHERE (content.type = 'pic')
ORDER BY v.voteCount DESC
When you do an INNER JOIN with votes table directly, if you have multiple occurrences of the same id, you will get a lot more rows than before you did the JOIN.
If you are only interested in the number of votes for each id, by doing a JOIN with a subquery that calculates the count of votes for each id, will leave your previous query results as they were, and lets you use the voteCount to order by it.
You could try this:
SELECT c.id, c.type, c.title, c.url, u.username, COUNT(v.id) votes
FROM content c
INNER JOIN users u ON c.uploaderuid = u.id
INNER JOIN votes v ON v.id = c.id
WHERE c.type = 'pic'
GROUP BY c.id, c.type, c.title, c.url, u.username
ORDER BY votes DESC
Here is the SQL Fiddle that demonstrates the below query:
SELECT c.id, c.type, c.title, c.url, u.username, COUNT(v.id)
FROM content AS c
INNER JOIN users AS u ON c.uploaderuid = u.id
INNER JOIN votes AS v ON v.id = c.id
WHERE c.type = 'pic'
GROUP BY c.id, c.type, c.title, c.url, u.username
ORDER BY COUNT(v.id) DESC

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.

Categories