Myql/PHP Problem $20 for solution

Discussion in 'Programming' started by selen, Dec 1, 2011.

  1. #1
    Hi
    I give $20 via paypal to the programmer who solves this problem, I wasn't able to do it, that's my code so far:

    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
    Code (markup):
    As you can see I order by sites.cpc DESC, now I try order by users.coins DESC as well, but users is another table. The two tables have sites.user = users.login

    So sites.cpc should be the first order and users.coins the second

    Why I need that, because a lot of sites have the same cpc and now I want the user with more coins comes before
     
    selen, Dec 1, 2011 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #2
    Your ORDER BY clause has only cpc. Try

    ORDER BY users.coins DESC, sites.cpc DESC
     
    Rukbat, Dec 1, 2011 IP
  3. selen

    selen Well-Known Member

    Messages:
    525
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    118
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #3
    Already tried that, it's not that simple I work since yesterday on this problem still haven't found a solution
     
    selen, Dec 1, 2011 IP
  4. proactiv3

    proactiv3 Peon

    Messages:
    55
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #4
    SELECT *
    FROM   `sites` AS s
           LEFT JOIN `users` AS u
             ON s.`user` = u.`login`
    WHERE  ( s.`banned` = '0'
             AND s.`user`!='{$data->login}' )
           AND s.`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 s.`cpc` DESC,
              u.`coins` DESC
    LIMIT  0, 10
    Code (markup):
    Double check for any syntax errors. If you require information that's on another table (other than the one you're affecting with the SELECT), you need to JOIN the tables in order to manipulate its values.

    Give it a try and leave some feedback. :)
     
    proactiv3, Dec 1, 2011 IP
  5. selen

    selen Well-Known Member

    Messages:
    525
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    118
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #5
    Yeah this looks good only id I had to make to s.id, but I still can't use the script. Can you tell me how I call the id object mysql_fetch_object because now I have two different ids. I call the id like $sit->id. I tried it like $sit->s.id, but this doesn't work
     
    selen, Dec 1, 2011 IP
  6. proactiv3

    proactiv3 Peon

    Messages:
    55
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #6
    It's the SELECT * part. If you have a same column value in both tables, they shall become indistinguishable.

    Ideally instead of SELECT * you would try something like this:

    SELECT s.id AS siteId, s.cpc AS siteCPC, u.id AS userId, u.coins AS userCoins ...
    Code (markup):
    SELECT *, s.id AS siteId, s.cpc AS siteCPC, u.id AS userId, u.coins AS userCoins ...
    Code (markup):
    Do this for the columns that have the same name on both tables. Then to fetch them its simple:

    <?php
    $db 	= new PDO("mysql:host={$db['host']};dbname={$db['name']}", $db['username'], $db['passwd']);
    $sql	= "SELECT s.id AS siteId, s.cpc AS siteCPC, u.id AS userId, u.coins AS userCoins ...";
    
    foreach($db->query($sql) as $row) {
    	$sideId 	= $row['siteId'];		// Contains s.id
    	$cpc		= $row['siteCPC'];		// Contains s.cpc
    	$userId		= $row['userId'];		// Contains u.id
    	$coins		= $row['userCoins'];	// Contains u.coins
    }
    PHP:
     
    proactiv3, Dec 2, 2011 IP
  7. karungandhi

    karungandhi Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #7
    hi,
    your all syntax is wrong and I tell you please use joint function to solve this problem


     
    karungandhi, Dec 7, 2011 IP
  8. Javed iqbal

    Javed iqbal Well-Known Member

    Messages:
    445
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #8
    your problem solved or still you need ?
     
    Javed iqbal, Dec 7, 2011 IP