I want to make these two into one SELECT statement:
SELECT *
FROM activegroupmodel
WHERE groupID = ?
ORDER BY groupModelID DESC
LIMIT 1
SELECT *
FROM model
WHERE modelID = ?
As you can see from the first groupID is not a pk and I want the latest model from that table.
I'v tried this but it obviously didn't work:
SELECT *
FROM activegroupmodel as a
WHERE a.groupID = ?
ORDER BY a.groupModelID DESC
LIMIT 1
INNER JOIN model as m ON a.modelID = m.modelID
I've read a bunch of questions with JOIN but none of them matches my problem.
Anybody knows how I can solve this?
Best regards Johan
just put your JOIN before the WHERE condition, like this:
SELECT *
FROM activegroupmodel as a
INNER JOIN model as m ON a.modelID = m.modelID
WHERE a.groupID = ?
ORDER BY a.groupModelID DESC
LIMIT 1
SELECT *
FROM activegroupmodel as a
INNER JOIN model as m ON a.modelID = m.modelID
WHERE a.groupID = ?
ORDER BY a.groupModelID DESC
LIMIT 1
The correct join statement should be as
SELECT *
FROM activegroupmodel as a
INNER JOIN model as m ON a.modelID = m.modelID
WHERE a.groupID = ?
ORDER BY a.groupModelID DESC
LIMIT 1
Try this
SELECT *
FROM activegroupmodel as a
INNER JOIN model as m ON a.modelID = m.modelID
WHERE a.groupID = ?
ORDER BY a.groupModelID DESC
LIMIT 1
Related
I have a query
SELECT s.*
, g.*
from tbl_section1 as s
, tbl_game as g
LEFT
JOIN tbl_game_visit_count AS gvc
ON g.game_id = gvc.game_id
where s.category_id = g.game_id
ORDER
BY g.udate DESC
, gvc.visit_count DESC
which works fine.
But I want to fetch the first record ordered by g.udate, and then the rest of the records ordered by gvc.visit_count.
Is this possible using mysql query?
Thanks in advance.
It could be possible by using UNION(not UNION ALL, since we don't want to duplicate rows ) between two queries with ORDER BY and LIMIT clauses inside parentheses
SELECT q.*
FROM
(
SELECT s.*, g.*
FROM tbl_section1 as s
INNER JOIN tbl_game as g ON s.category_id = g.game_id
LEFT JOIN tbl_game_visit_count AS gvc ON g.game_id = gvc.game_id
ORDER BY g.udate DESC
LIMIT 1
) q
UNION
SELECT s.*, g.*
FROM tbl_section1 as s
INNER JOIN tbl_game as g ON s.category_id = g.game_id
LEFT JOIN tbl_game_visit_count AS gvc ON g.game_id = gvc.game_id
ORDER BY gvc.visit_count DESC;
P.S. Because of your original query I kept DESC options for ORDER BY, you can get rid of them if you want regular ascending ordering.
I have sql in below, that want to make multiple ORDER BY.
SELECT
r.*,
s.uzunluq,
b.id,
au.status_id as aksessuar_status,
au.aksessuar_id,
au.aksessuar
FROM
seksiya s,
result r
LEFT JOIN bosh_seksiya_aksessuar b
ON
b.bosh_seksiya = r.model AND
b.ERK = :ses
LEFT JOIN aksessuar_up au
ON au.model_id = r.res_id AND
au.user_id = :user_id AND
au.status_id = 9
WHERE
r.user_id = :user_id AND
r.model=s.seksiya AND
s.erk = :ses AND
r.status_id IN (1,2,3,4,5)
ORDER BY
r.res_id
I think to write php PDO is not important for you, guys, cause my question only about with this sql. This sql works very good, I just want to add extra function. So, look to this column: r.status_id IN (1,2,3,4,5)
I have given Order BY r.res_id
MY question:
I want to use multiple ORDER for each status_id
HOW to order:
ORDER BY r.res_id DESC WHERE r.status_id IN (1,2)
AND
ORDER BY r.res_id WHERE r.status_id IN (3,4,5)
in this sql?
ORDER BY IF(r.status_id IN (1,2), r.res_id, NULL) DESC, r.res_id
A recordset sorted with this ORDER BY clause will first display all records with r.status_id IN (1,2) (since NULL values come last in a descending ordering), themselves sorted in descending order of r.res_id; followed by all other values sorted by r.res_id in ascending order.
you can use union syntax:
SELECT * FROM ((SELECT
r.*,
s.uzunluq,
b.id,
au.status_id as aksessuar_status,
au.aksessuar_id,
au.aksessuar,
1 as query_order
FROM
seksiya s,
result r
LEFT JOIN bosh_seksiya_aksessuar b
ON
b.bosh_seksiya = r.model AND
b.ERK = :ses
LEFT JOIN aksessuar_up au
ON au.model_id = r.res_id AND
au.user_id = :user_id AND
au.status_id = 9
WHERE
r.user_id = :user_id AND
r.model=s.seksiya AND
s.erk = :ses AND
r.status_id IN (1,2)
ORDER BY
r.res_id DESC) as table1
UNION
(SELECT
r.*,
s.uzunluq,
b.id,
au.status_id as aksessuar_status,
au.aksessuar_id,
au.aksessuar,
0 as query_order
FROM
seksiya s,
result r
LEFT JOIN bosh_seksiya_aksessuar b
ON
b.bosh_seksiya = r.model AND
b.ERK = :ses
LEFT JOIN aksessuar_up au
ON au.model_id = r.res_id AND
au.user_id = :user_id AND
au.status_id = 9
WHERE
r.user_id = :user_id AND
r.model=s.seksiya AND
s.erk = :ses AND
r.status_id IN (3,4,5)
ORDER BY
r.res_id) as table2) sa table3 ORDER BY query_order
I'm trying to get all topics along with the last comment in each topic. I've tried a couple of different sql statements, which haven't been working out.
SELECT
a.*,
b.*,
c.*,
(SELECT COUNT(*) FROM comments WHERE comment_topic_id = a.topic_id) AS count
FROM topics AS a
LEFT JOIN categories AS b ON a.topic_category = b.category_id
LEFT JOIN (
SELECT *
FROM comments
ORDER BY comment_date DESC
) AS c ON a.topic_id = c.comment_topic_id
WHERE b.category_id = '1' AND b.category_permission <= '2'
ORDER BY a.topic_created ASC
The above code will generate a result for each comment instead of the most recent.
Any help is appreciated, I can provide images to illustrate the database and table structures
I've changed the alias of your count because count is a reserved word.
Try this:
EDIT
SELECT
a.*,
b.*,
co.*,
(SELECT COUNT(*) FROM comments WHERE comment_topic_id = a.topic_id) AS tot_comment
FROM topics AS a
JOIN categories AS b ON a.topic_category = b.category_id
LEFT JOIN (
SELECT *
FROM comments c
WHERE NOT EXISTS(
SELECT 'NEXT'
FROM comments c2
WHERE c2.comment_topic_id = c.comment_topic_id
AND c2.comment_date > c.comment_date
)
) AS co ON a.topic_id = co.comment_topic_id
WHERE b.category_id = '1' AND b.category_permission <= '2'
ORDER BY a.topic_created ASC
I want to select a "site" record(s) randomly, and then get the related "channels" for it. Here's what I've tried that doesn't work. Please help. Thank you!
SELECT A.*, B.*
FROM (
SELECT companies.company_name, sites.id
FROM sites
INNER JOIN company_sites ON company_sites.site_id = sites.id
INNER JOIN companies ON companies.id = company_sites.company_id
WHERE sites.active = 1
AND sites.stage_id = 5
GROUP BY sites.id
ORDER BY RAND()
LIMIT 1
)A
JOIN (
SELECT
channels.id
FROM channels
WHERE channels.site_id = A.sites_id
) B ON 1 = 1
Try this:
SELECT just the fields you need
FROM (
SELECT companies.company_name, sites.id id
FROM sites
INNER JOIN company_sites ON company_sites.site_id = sites.id
INNER JOIN companies ON companies.id = company_sites.company_id
WHERE sites.active = 1
AND sites.stage_id = 5
GROUP BY sites.id
ORDER BY RAND()
LIMIT 1
)A
join channels on channels.site_id = id
By the way, if you just wanted the list of channels on each site, you can concatenate them into one field (rather than getting them on separate rows). The query would be simpler:
SELECT companies.company_name, sites.id, group_concat(channels.id) as channels
FROM sites
INNER JOIN company_sites ON company_sites.site_id = sites.id
INNER JOIN companies ON companies.id = company_sites.company_id
inner join channels on channels.site_id = sites.id
WHERE sites.active = 1
AND sites.stage_id = 5
GROUP BY sites.id
ORDER BY RAND()
LIMIT 1
I would like to order my posts by their timestamp but that doesn't and I don't know why.
When I execute my query without order, it works, but when I add ORDER BY it doesn't work.
This is my query :
SELECT * FROM Posts INNER JOIN Topics ON idTopic = idTopics
INNER JOIN Users ON idUserPosts = idUsers
WHERE idTopic = :idTopic LIMIT :limit_down, :limit_up
ORDER BY Posts.datePosts DESC;
Why that doesn't work?
Try inversing the ORDER BY/LIMIT:
SELECT * FROM Posts
INNER JOIN Topics ON idTopic = idTopics
INNER JOIN Users ON idUserPosts = idUsers
WHERE idTopic = :idTopic
ORDER BY Posts.datePosts DESC
LIMIT :limit_down, :limit_up;
The order by needs to be before the limit, else the limit is way less useful: you'd get the X first rows of the table, but "first" using what criteria? And then you'd order those results.
You can do that, too, if you really want to, but think about what you are doing then:
SELECT * FROM (
SELECT Posts.datePosts orderField, * FROM Posts
INNER JOIN Topics ON idTopic = idTopics
INNER JOIN Users ON idUserPosts = idUsers
WHERE idTopic = :idTopic
LIMIT :limit_down, :limit_up;
)
ORDER BY orderField DESC
Try this:
SELECT * FROM Posts INNER JOIN Topics ON idTopic = idTopics
INNER JOIN Users ON idUserPosts = idUsers
WHERE idTopic = :idTopic
ORDER BY Posts.datePosts DESC
LIMIT :limit_down, :limit_up;
Limit must be the last thing applied to a query, so order by needs to come first.
Seems dateposts is a date field then why not try this
SELECT * FROM Posts INNER JOIN Topics ON idTopic = idTopics
INNER JOIN Users ON idUserPosts = idUsers
WHERE idTopic = :idTopic LIMIT :limit_down, :limit_up
ORDER BY DATE(Posts.datePosts) DESC;