Select from 2 tables and order by 1 - php

I have 2 tables.
First:
TABLE Articles
ID
Second:
TABLE Viewcount
DATE DATE PK
ARTICLE_ID INT PK (MATCHES ID IN TABLE Articles)
NUMVIEWS INT
How do I select all IDs from table Articles and then order by NUMVIEWS (DESC) of Viewcount according to dates?
I then need to append the IDs from Articles that were not found in viewcount to the End of the results in no particular order.
I know it has to be some sort of Join but I can't figure it out..

try this
SELECT id from Articles a
LEFT JOIN Viewcount v
ON a.id = v.article_id
AND v.date = 'some date here'
ORDER BY v.numviews ,v.date desc

A simple join will suffice, yes:
SELECT a.id FROm Articles a LEFT JOIN Viewcount v
ON v.article_id = a.id
ORDER BY v.numviews desc, v.date

This should work:
SELECT * FROM `Articles` `a`, `Viewcount` `v`
WHERE `v`.`ARTICLE_ID`=`a`.`ID`
ORDER BY `v`.`NUMVIEWS` DESC
Replace SELECT * by SELECT `a`.`ID` to get only the Article IDs.

SELECT ID from (Articles JOIN Viewcount on Articles.ID = Viewcount.ID) ORDER BY Viewcount.NUMVIEWS, Viewcount.date

Related

Count number of rows for specific ID

In first table album has id and second table album_details has sub_id which relates from album table id
I need to display count for separate id value.
SELECT DISTINCT B.SUB_ID, A . * , B.CONTENT_VALUE AS detail,
(SELECT COUNT( ID )
FROM album_details WHERE A.ID = B.SUB_ID ) AS count
FROM album AS A, album_details AS B
WHERE A.WEBSITE_ID = '571710720'
AND A.ID = B.SUB_ID
GROUP BY B.SUB_ID
LIMIT 0 , 30
Now count column shows 40 for all rows but need to display 'count' 6 for 'id=4', 'count' 3 for 'id=2'
SELECT count(SUB_ID),SUB_ID from album_details group by SUB_ID
GROUP BY is your weapon of choice.
SELECT
a.ID,
a.CONTENT_VALUE,
COUNT(ad.ID)
FROM albums AS a
LEFT JOIN album_details AS ad ON a.ID = ad.SUB_ID
GROUP BY a.ID
Feel free to add your WHERE before the GROUP BY.
Lets say first table is A and second table is B then query will be like this
select a.ID, count(b.SUB_ID) AS total
FROM A LEFT JOIN B ON A.ID = B.SUB_ID
Group by B.SUB_ID.
It might help you. If not then ask please.
select count(sub_id) as count1 from album_details where sub_id in(select id from album) WHERE album.WEBSITE_ID = '571710720'
AND album.ID = album_details.SUB_ID

Mysql - get last post from category

I have this structure (tables) of forum
I want to select last post (row from forum_post table) from category.
SQL so far:
SELECT * FROM table_post
WHERE topic_id = (SELECT MAX(id) FROM table_topic WHERE category_id = {$id})
ORDER BY id ASC LIMIT 1
Question: How to modify this select to achieve my goal?
Assuming that "last" means the biggest id, I would suggest order by and limit:
select fp.*
from forum_post fp join
forum_topic ft
on fp.topic_id = ft.id
where ft.category_id = $id
order by fp.id desc
limit 1;

PHP: 2 tables - Maximum id2

I want to get all id with the max id2 value.
I tried just to get the max id2 but then it will looks for the overall maximum value of id2 inside the table , but i want to get all maxiums of id.
So I got 2 tables - table news and table topics.
Everytime I create a news there will automaticly create a topic. Now I want to show all news - and the current number of replies. So first step - topicid = id.
and every topic got id and id2.
id is the topic id
and id2 is the reply id
so if i got topic (a) with 4 comments it would look like
(id(1),id2(1))
(id(1),id2(2))
(id(1),id2(3))
(id(1),id2(4))
now a new topic (b) with 6 comments
(id(2),id2(1))
(id(2),id2(2))
(id(2),id2(3))
(id(2),id2(4))
(id(2),id2(5))
(id(2),id2(6))
so i want to get ((id(1),id2(4)) and (id(2),id2(6)))
<?php
$news = "SELECT n.titel,n.datum,n.typ_news,n.news,n.verfasser,n.time,n.topicid,
t.id, t.id2 FROM news n LEFT JOIN topics t ON t.id = n.topicid
ORDER BY n.id DESC LIMIT 10 ";
$neuenews = mysql_query($news);
while ($dnews = mysql_fetch_array($neuenews))
{
echo " <div style='text-align:center;color:#FFFFFF;font-size: 24px;'> "
.$dnews['titel'].
"a";
}
Ehm this was the solution :
$dn1 = mysql_query('select c.id, c.name, c.description, c.position,c.bild,
(select count(t.id) from topics as t where t.parent=c.id and t.id2=1) as topics,
(select count(t2.id) from topics as t2 where t2.parent=c.id and t2.id2!=1) as replies
from categories as c group by c.id order by c.position asc');
Try this:
SELECT n.titel,n.datum,n.typ_news,n.news,n.verfasser,n.time,n.topicid, t.id, MAX(t.id2) AS id2
FROM news n
LEFT JOIN
topics t
ON t.id = n.topicid
GROUP BY n.topicid
ORDER BY n.id DESC LIMIT 10
It looks like the specified/desired result from the topics table is accomplished by a query like this:
SELECT t.id
, MAX(t.id2) AS max_id2
FROM topics t
GROUP BY t.id
OPTION 1
To get that result joined to rows in news, you could use that query as an inline view in your query in place of the topics table. For example:
SELECT n.titel
, n.datum
, n.typ_news
, n.news
, n.verfasser
, n.time
, n.topicid
, t.id
, t.max_id2
FROM news n
LEFT
JOIN ( SELECT m.id
, MAX(m.id2) AS max_id2
FROM topics m
GROUP BY m.id
) t
ON t.id = n.topicid
ORDER BY n.id DESC LIMIT 10
OPTION 2
If id is UNIQUE (or PRIMARY KEY) in news table, then you may be able to eliminate the inline view, do a join to topics, and do a GROUP BY n.id, something like this:
SELECT n.titel
, n.datum
, n.typ_news
, n.news
, n.verfasser
, n.time
, n.topicid
, t.id
, MAX(t.id2) AS max_id2
FROM news n
LEFT
JOIN topics t
ON t.id = n.topicid
GROUP BY n.id
ORDER BY n.id DESC LIMIT 10
Not clear enough. what are the columns id= 1 id2=1 etc.? Two tables each with 2 columns? No clue.
I'm thinking something on the order but No clue what you actually want.
SELECT `MAX(`id2`) as MAX,`id` FROM `News` WHERE `id2` = `MAX`

I am stuck on one query in mysql

I am stuck on one query in mysql.
I want to fetch most recent comment from the table
the comment should be most recent comment on the blog
the blogs should be latest 3 blogs.
display comment & blog only if their status is Enabled
records should be like this
Table Structure for the table the table blog
blog_id int - primary (auto increment)
blog_title -varchar
blog_desc -varchar
blog_image -varchar
blog_tags -varchar
tot_comments -int
blog_creater -varchar
blog_create_date -datetime
blog_status -enum ('Enable','Disable')
table structure for the table blog_comment
comment_id -int (auto increment)
fk_blog_id -int
comment -varchar
comment_by -varchar
email -varchar
comment_date -datetime
comment_status -enum ('Enable','Disable')
And below is query written by me, but the result I am getting is wrong.
SELECT b.blog_title,b.blog_image, bc.*
FROM blog_comments bc, blog b
WHERE bc.comment_status='Enable'
AND b.blog_status='Enable'
AND b.blog_id=bc.fk_blog_id
GROUP BY bc.fk_blog_id
ORDER BY bc.comment_date DESC
LIMIT 0,3
Output
for this the easy solution will be execute 2 query for your result . first query get blog post result
$db_blog="select blog_id,blog_title from blog where blog_ststus='Enable'";
$que=mysql_query($db_blog);
while($row=mysql_fetch_object($que))
{
echo $row->blog_title;
$db_comment="select comment from blog_comments where fk_blog_id=".$row->blog_id." and comment_status='Enable' order by comment_date desc";
$quec=mysql_query($db_comment);
while($comment=mysql_fetch_object($quec))
{
echo $comment->comment;
}
}
Try this:
SELECT * FROM blog_comments bc, blog b
WHERE `bc.comment_status`='Enable'
AND `b.blog_status`='Enable'
AND `b.blog_id`=bc.fk_blog_id
ORDER BY `bc.comment_date` DESC LIMIT 1;
Try a simpler one:
SELECT * FROM `blog_comment` WHERE 'blog_status'='Enable' AND 'blog_id'='$blogidherefromtitle' ORDER BY 'comment_date' DESC LIMIT1
SELECT b.blog_title,b.blog_image, bc.*
FROM blog b
left join (
Select * from
blog_comments bc
WHERE bc.comment_status='Enable'
GROUP BY bc.fk_blog_id
having max(bc.comment_date) = bc.comment_date
) bcc on b.blog_id=bcc.fk_blog_id
where
b.blog_status='Enable'
ORDER BY b.blog_create_date desc
LIMIT 0,3
Try this one
try
SELECT b.blog_title,b.blog_image, bc.*
FROM blog_comments AS bc, blog AS b
WHERE bc.comment_status='Enable'
AND b.blog_status='Enable'
AND b.blog_id=bc.fk_blog_id
GROUP BY bc.fk_blog_id
ORDER BY bc.comment_date DESC
LIMIT 0,3;
(I'm not 100% sure)
SELECT b.blog_title,b.blog_image, bc.*
FROM blog_comments bc JOIN blog b ON bc.fk_blog_id = b.blog_id
WHERE bc.comment_status='Enable'
AND b.blog_status='Enable'
GROUP BY bc.fk_blog_id
ORDER BY bc.comment_date DESC
LIMIT 0,3
select b.blog_title, b.blog_image, bc.*
from blog b join
(select bc.*
from bc join (select fk_blog_id, max(comment_date) latest_date
from blog_comment
where comment_status = 'Enable'
group by fk_blog_id) latest
on bc.fk_blog_id = latest.fk_blog_id and bc.comment_date = latest_date) c
on b.blog_id = c.fk_blog_id
where b.blog_status = 'Enable'
order by c.comment_date desc
limit 0, 3
The c subquery finds the row with the latest comment for each blog, using the technique in the linked question. This is then joined with the blog table to get the appropriate blog data.
Try this query
SELECT
bc.*
FROM
blog AS b
INNER JOIN (SELECT id , MAX(id) AS MaxID FROM blog) AS bl ON bl.id = b.id
LEFT JOIN blog_comment AS bc ON bc.fk_blog_id = b.id
ORDER BY bc.comment_id DESC
LIMIT 3
EDITS:
SELECT
bc.*
FROM
blog AS b
INNER JOIN (SELECT id , MAX(id) AS MaxID FROM blog GROUP BY id) AS bl ON bl.id = b.id
INNER JOIN (SELECT MAX(id) , fk_blog_id FROM blog_comment GROUP BY id) AS bc ON bc.fk_blog_id = b.id
ORDER BY bc.comment_id DESC
LIMIT 3
This is for 3 latest blogs and latest single comments for each blog
Here using inner join will fetch the latest blog. than join comments and order them with date or id and limit them according to your requirements.

MySQL query order the results in GROUP BY 3 Tables Count

I'm coding a listing system and I'm trying to get the posts ORDER by number of comments and votes FROM 2 tables.
Table1 : Lists => id, title, detail
Table2 : Votes => voteid, listid
Table3 : Comments => commentid, listid
WHERE MY Current query is
$q = mysql_query("SELECT * FROM zoo_leads
LEFT JOIN Votes ON Lists.id=Votes.listid
LEFT JOIN Comments ON Lists.id=Comments.listid
GROUP BY Lists.id ORDER BY Comments.listid DESC LIMIT 10
it is showing me results perfectly but ORDER BY is Lists.id Instead of number of votes and comments
Try:
SELECT *
FROM zoo_leads
LEFT JOIN votes
ON lists.id = votes.listid
LEFT JOIN comments
ON lists.id = comments.listid
GROUP BY lists.id
ORDER BY COUNT(votes.id) DESC,
COUNT(comments.id) DESC
LIMIT 10
That is because you have ORDER BY Comments.listid in your SQL statement.

Categories