This is litle complicated query for me, but if anyone have idea please post id...
I want to make something like this
SITE 1
post
post
post
SITE 2
post
post
post
here is my query
$result = mysql_query("SELECT content.* FROM content WHERE content.site IN
(SELECT site_id FROM site_subscription WHERE user_id = ".$_SESSION['userid'].")
ORDER by content.site DESC");
while($row = mysql_fetch_assoc($result)) {
$array[] = $row;
}
AND im display post
if(is_array($array)) {
foreach($array as $row) {
include($basepath.'/template/item.php');
}
}
This is working and post is sorted by site_id, but need to get site_name from other table, table name site and than GROUP post by site_name like above SITE 1, SITE 2, SITE 3
Something like SELECT * FROM site WHERE id='content.site'
"SELECT * FROM content INNER JOIN site ON content.site = site.id
WHERE site.id IN (
SELECT site_id FROM site_subscription WHERE user_id = ".$_SESSION['userid'].")
ORDER BY site.name"
$result = mysql_query("SELECT content.* FROM content
INNER JOIN site ON content.site = site.id
WHERE content.site IN
(SELECT site_id FROM site_subscription WHERE user_id = ".$_SESSION['userid'].")
ORDER by content.site DESC");
Do a JOIN
instead of subquery. Try something like this:
SELECT content.*, site_name, FROM content
JOIN site_subscription ON content.site = site_id
WHERE site_subscription.user_id = <USER_ID>
ORDER by content.site DESC
GROUP BY site_name
MysSQL will handle this query but it's not fully correct. You can't actually select content.*
as you have to select only aggregated fields while you are grouping rows.