Jump to content

Multiple tables with multiple order by


selenin

Recommended Posts

Hi

I have a question, my script so far

 

  $site2 = mysql_query("SELECT * FROM `sites` WHERE (`banned` = '0' AND `user`!='{$data->login}') AND  sites.user IN (select concat_ws(',', login) from users WHERE `coins`>=`cpc`) AND `id` NOT IN (SELECT `site_id` FROM `plused` WHERE `user_id`='{$data->id}') ORDER BY `cpc` DESC LIMIT 0, 10");
  $ext = mysql_num_rows($site2);

 

Now it's ordered by cpc desc, but I want to order as well by from the table users coins desc. Then the user with more coins is before the other. But sites.cpc is another table then users.coins, is this possible?

Link to comment
Share on other sites

Don't know where to edit my post

 

like that?

 

  $site2 = mysql_query("SELECT * FROM `sites` WHERE (`banned` = '0' AND `user`!='{$data->login}') AND  sites.user IN (select concat_ws(',', login) from users WHERE `coins`>=`cpc`) AND `id` NOT IN (SELECT `site_id` FROM `plused` WHERE `user_id`='{$data->id}') ORDER BY `cpc` DESC LIMIT 0, 10");
Link to comment
Share on other sites

You can order by as many expressions as you'd like.

 

$site2 = mysql_query("SELECT * FROM `sites` WHERE (`banned` = '0' AND `user`!='{$data->login}') AND  sites.user IN (select concat_ws(',', login) from users WHERE `coins`>=`cpc`) AND `id` NOT IN (SELECT `site_id` FROM `plused` WHERE `user_id`='{$data->id}') ORDER BY `cpc` DESC LIMIT 0, 10");

 

Yes I know but I want as well order by coins, but coins is from another table, the table users. First it should be ordered by cpc desc and sometimes the cpc are the same the it should be ordered by users.coins

Link to comment
Share on other sites

SELECT * FROM `sites` JOIN users 
          ON sites.user = users.login WHERE (`banned` = '0' AND `user`!='{$data->login}') AND  sites.user IN (select concat_ws(',', login) from users WHERE `coins`>=`cpc`) AND `id` NOT IN (SELECT `site_id` FROM `plused` WHERE `user_id`='{$data->id}') ORDER BY `cpc` DESC LIMIT 0, 10

 

I tried it like that but I'm only getting error messages, I really don't understand this JOIN

Link to comment
Share on other sites

Yes the error message is simple, because I'm not getting any results, normally I get 10

 

Error message

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/selenin/public_html/artmeo/google.php on line 5

Then show us the output of mysql_error().

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.