Jump to content

SELECT, GROUP, ORDER BY, JOIN all in one query


dreamwest

Recommended Posts

Im trying to make a query to tell how many videos each user has uploaded, but the there are two tables and i need to sort by amount of videos descending

 

Heres what ive done

$query = "SELECT videos.*, signups.* ".
"FROM videos, signups ".
"WHERE videos.user_id = signups.user_id limit 5 ";

$result = mysql_query($query) or die(mysql_error());


// Print out the contents of each row into a table 
while($row = mysql_fetch_array($result)){
echo $row['username']. " has ". $row['   ']." videos";
echo "<br />";
}

 

Heres my table structure

 

Table1:

signups (tabe name)

 

user_id  |  username

 

Table 2:

videos (Table name)

 

id |  user_id 

 

 

I proberly need a count or group in here but im uncertain

 

Link to comment
Share on other sites

Thanks. The query works,  but im still having trouble with the while loop

 

while($row = mysql_fetch_array($result)){
echo $row['username']." has ".$row['COUNT(id)']." videos";
echo "<br />";
}

 

The username is grouped and echos but doesnt have any video count

Link to comment
Share on other sites

Thanks. The query works,  but im still having trouble with the while loop

 

while($row = mysql_fetch_array($result)){
echo $row['username']." has ".$row['COUNT(id)']." videos";
echo "<br />";
}

 

The username is grouped and echos but doesnt have any video count

Put $row[1]

 

Instead of

 

$row['COUNT(id)']

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.