mforan Posted April 13, 2008 Share Posted April 13, 2008 how would one go about selecting data from the database which is present 2 (or more) times in the database? Quote Link to comment https://forums.phpfreaks.com/topic/100920-solved-selecting-multple-doubles/ Share on other sites More sharing options...
Barand Posted April 13, 2008 Share Posted April 13, 2008 use a subquery to find multiple values then select using a join matching aginst the subquery. Quote Link to comment https://forums.phpfreaks.com/topic/100920-solved-selecting-multple-doubles/#findComment-516228 Share on other sites More sharing options...
mforan Posted April 13, 2008 Author Share Posted April 13, 2008 im confused already lol, i think i understand what you are saying thou, im not sure about this subquery :S Quote Link to comment https://forums.phpfreaks.com/topic/100920-solved-selecting-multple-doubles/#findComment-516277 Share on other sites More sharing options...
Barand Posted April 13, 2008 Share Posted April 13, 2008 If you ask a generalised question I can only give a generalised answer. Quote Link to comment https://forums.phpfreaks.com/topic/100920-solved-selecting-multple-doubles/#findComment-516337 Share on other sites More sharing options...
Barand Posted April 15, 2008 Share Posted April 15, 2008 I can be more specific but I have to invent an example. I'm not sure how relevant this is to your situation. Anyway, it's a two-for-price-of-one offer Prices data [pre] +----------+-------+ | price_id | price | +----------+-------+ | 1 | 29.95 | | 2 | 35.00 | | 3 | 25.00 | | 4 | 25.00 | | 5 | 20.00 | | 6 | 15.00 | | 7 | 15.00 | | 8 | 12.00 | | 9 | 10.50 | +----------+-------+[/pre] Find rows with the same prices. [pre] mysql> SELECT a.price_id as itemA, b.price_id as itemB -> FROM prices a INNER JOIN prices b ON a.price=b.price -> WHERE a.price_id < b.price_id; +-------+-------+ | itemA | itemB | +-------+-------+ | 3 | 4 | | 6 | 7 | +-------+-------+ [/pre] Find rows with duplicate prices [pre] mysql> SELECT p.price_id -> FROM prices p -> JOIN (SELECT price, COUNT(*) FROM prices GROUP BY price -> HAVING COUNT(*) > 1) as x -> ON p.price=x.price; +----------+ | price_id | +----------+ | 3 | | 4 | | 6 | | 7 | +----------+ Quote Link to comment https://forums.phpfreaks.com/topic/100920-solved-selecting-multple-doubles/#findComment-518053 Share on other sites More sharing options...
mforan Posted April 17, 2008 Author Share Posted April 17, 2008 ahh perfect, after a bit of tweaking that worked a charm, and i learnt from this about the "group by" and "having count", understand now how they work abit more! cheers bud! Quote Link to comment https://forums.phpfreaks.com/topic/100920-solved-selecting-multple-doubles/#findComment-519883 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.