Jump to content

mysql subquery not working.


MDanz

Recommended Posts

I'm trying to do a query that if mike isn't in the three highest bids for a keyword, then display the row(row that has been outbid).  I tried below but i get no results.  I should get rows with id 4 and 7. btw a keyword can be bidded on more than once.

 

$construct = "SELECT * FROM `temp-advertise` WHERE username='mike' AND bid <
			(SELECT min.bid FROM `temp-advertise` min LEFT JOIN `temp-advertise` min2 on min.keyword=min2.keyword WHERE min.username='mike' ORDER BY bid DESC LIMIT 2,1)";


id  | username| keyword | bid   |
   1 |  mike |  one     |  7    |
   2 |  tomm |  one     |  4    |
   3 |  cedr |  one     |  6    |
   4 |  mike |  two     |  1    |
   5 |  tomm |  two     |  5    |
   6 |  harr |  two     |  5    |
   7 |  mike |  one     |  3    |
   8 |  harr |  two     |  3    |

 

i tried another query and also get no results

 

$construct = "SELECT child.* FROM `temp-advertise` child LEFT JOIN `temp-advertise` parent on child.keyword=parent.keyword
			WHERE child.username='$username' GROUP BY child.keyword ORDER BY child.bid DESC LIMIT 2,999";

Link to comment
Share on other sites

SELECT `ta`.*, COUNT(`ta2`.`id`) AS `outbids`

FROM `temp-advertise` AS `ta`

INNER JOIN `temp-advertise` AS `ta2`

    ON `ta`.`keyword` = `ta2`.`keyword`

    AND `ta`.`bid` < `ta2`.`bid`

    AND `ta`.`username` != `ta2`.`username`

WHERE `username` = 'mike'

GROUP BY `ta`.`id`

HAVING `outbids` >= 3;

 

EDIT: made it an inner join.... you don't need a left join here.

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.