PeerFly Posted January 20, 2009 Share Posted January 20, 2009 I'm really getting frustrated about this issue. The problem is that I am trying to join 2 tables and then sort by a certain field. I don't know enough about joins to make this work and I've googled until my head exploded. mysql_query("select * from `offers` where `status` = 'On' ORDER BY `pay_value` ASC"); I need to pull the "pay_value" from 2 different tables and then order it by that only. Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/141565-solved-php-mysql-order-by-field-from-other-table/ Share on other sites More sharing options...
fenway Posted January 20, 2009 Share Posted January 20, 2009 how about some table structure? Quote Link to comment https://forums.phpfreaks.com/topic/141565-solved-php-mysql-order-by-field-from-other-table/#findComment-741010 Share on other sites More sharing options...
PeerFly Posted January 20, 2009 Author Share Posted January 20, 2009 Ok, the first table is called `offers` and the field I'm referencing is `pay_value` type decimal(10,2) and the second table is called `exceptions` with a `pay_value` type decimal(10,2). What I'm doing is showing a list of ads to our clients with the amount of money that each ad pays per lead/sale. Some of our clients have been given increased payouts. Well, when they go to sort all of the ads in the system via the 'Pay Value' I can't make it show the increased payouts from the `exceptions` table while at the same time, being in correct order with the other `pay_value`'s from the `offers` table. I hope that doesn't sound too confusing. Quote Link to comment https://forums.phpfreaks.com/topic/141565-solved-php-mysql-order-by-field-from-other-table/#findComment-741011 Share on other sites More sharing options...
fenway Posted January 20, 2009 Share Posted January 20, 2009 So you want to show both prices? Quote Link to comment https://forums.phpfreaks.com/topic/141565-solved-php-mysql-order-by-field-from-other-table/#findComment-741286 Share on other sites More sharing options...
PeerFly Posted January 20, 2009 Author Share Posted January 20, 2009 I just want to join the tables and sort by the pay_value. But yeah, I want the prices from both tables to show. Quote Link to comment https://forums.phpfreaks.com/topic/141565-solved-php-mysql-order-by-field-from-other-table/#findComment-741384 Share on other sites More sharing options...
fenway Posted January 20, 2009 Share Posted January 20, 2009 It's far from ideal to join on a non-integer value -- particularly on a variable value, like price. select o.pay_value as orig, e.pay_value as new from offers as o left join exceptions as e using ( pay_value ) where o.status = 'On' order by o.pay_value ASC Quote Link to comment https://forums.phpfreaks.com/topic/141565-solved-php-mysql-order-by-field-from-other-table/#findComment-741582 Share on other sites More sharing options...
PeerFly Posted January 20, 2009 Author Share Posted January 20, 2009 Ok fen, you got me started in the right direction, thank you! One thing I didn't mention was the pay_value from the exceptions table only needs to show if the offerid is shown in the exceptions table. I modified the sql a bit, but it doesn't seem to work... select o.offerid, o.name, o.incent, o.type, o.reporting, o.category, o.pay_value as orig, e.pay_value as new from offers as o left join exceptions as e using (pay_value) where o.status = 'On' and e.offerid = o.offerid order by o.pay_value $orderx LIMIT $from, $max_results I added in more selected fields: o.offerid, o.name, o.incent, o.type, o.reporting, o.category And: e.offerid = o.offerid It seems like it would work but the result is no fields are shown even though there are 3 payout exceptions in the exceptions table. Quote Link to comment https://forums.phpfreaks.com/topic/141565-solved-php-mysql-order-by-field-from-other-table/#findComment-741620 Share on other sites More sharing options...
PeerFly Posted January 21, 2009 Author Share Posted January 21, 2009 Anyone? Fenway? Quote Link to comment https://forums.phpfreaks.com/topic/141565-solved-php-mysql-order-by-field-from-other-table/#findComment-741898 Share on other sites More sharing options...
fenway Posted January 21, 2009 Share Posted January 21, 2009 Try: select o.offerid , o.name, o.incent, o.type, o.reporting, o.category , IFNULL( e.pay_value, o.pay_value ) AS pay_value from offers as o left join exceptions as e using (pay_value,offerid) where o.status = 'On' order by o.pay_value $orderx LIMIT $from, $max_results Quote Link to comment https://forums.phpfreaks.com/topic/141565-solved-php-mysql-order-by-field-from-other-table/#findComment-742629 Share on other sites More sharing options...
PeerFly Posted January 22, 2009 Author Share Posted January 22, 2009 Ok, well... no errors but it seems to only be sorting from the offers table and totally ignoring the exceptions table. It's acting like this: select * from `offers` where `status` = 'On' ORDER BY `pay_value` $orderx LIMIT $from, $max_results Quote Link to comment https://forums.phpfreaks.com/topic/141565-solved-php-mysql-order-by-field-from-other-table/#findComment-742790 Share on other sites More sharing options...
PeerFly Posted January 23, 2009 Author Share Posted January 23, 2009 Can anyone else help with the problem? Fenway seems like a busy man but I'm sure there are more mysql know-it-alls on this board! Quote Link to comment https://forums.phpfreaks.com/topic/141565-solved-php-mysql-order-by-field-from-other-table/#findComment-744090 Share on other sites More sharing options...
PeerFly Posted January 27, 2009 Author Share Posted January 27, 2009 Is this even possible? Should I sort this row with javascript and give up on the mysql sorting of this row? Quote Link to comment https://forums.phpfreaks.com/topic/141565-solved-php-mysql-order-by-field-from-other-table/#findComment-747357 Share on other sites More sharing options...
fenway Posted January 27, 2009 Share Posted January 27, 2009 Now i'm confused... what are you trying to order by? Quote Link to comment https://forums.phpfreaks.com/topic/141565-solved-php-mysql-order-by-field-from-other-table/#findComment-747562 Share on other sites More sharing options...
PeerFly Posted January 30, 2009 Author Share Posted January 30, 2009 Now i'm confused... what are you trying to order by? I'm trying to order by the value of the offer from the offers table UNLESS there is an entry in the exceptions table with that offerid. If there is an exception it will be lower or higher than the default value (because it's an exception to the default value) so it needs to correctly sort with the other values. Quote Link to comment https://forums.phpfreaks.com/topic/141565-solved-php-mysql-order-by-field-from-other-table/#findComment-750473 Share on other sites More sharing options...
DeanWhitehouse Posted January 31, 2009 Share Posted January 31, 2009 try SELECT o.offerid , o.name, o.incent, o.type, o.reporting, o.category , IFNULL( e.pay_value, o.pay_value ) AS pay_value FROM offers as o JOIN exceptions as e USING (pay_value,offerid) WHERE o.status = 'On' ORDER BY o.pay_value $orderx LIMIT $from, $max_results Quote Link to comment https://forums.phpfreaks.com/topic/141565-solved-php-mysql-order-by-field-from-other-table/#findComment-751228 Share on other sites More sharing options...
fenway Posted February 2, 2009 Share Posted February 2, 2009 try SELECT o.offerid , o.name, o.incent, o.type, o.reporting, o.category , IFNULL( e.pay_value, o.pay_value ) AS pay_value FROM offers as o JOIN exceptions as e USING (pay_value,offerid) WHERE o.status = 'On' ORDER BY o.pay_value $orderx LIMIT $from, $max_results Actually, I think you mean: SELECT o.offerid , o.name, o.incent, o.type, o.reporting, o.category , IFNULL( e.pay_value, o.pay_value ) AS payValue FROM offers as o JOIN exceptions as e USING (pay_value,offerid) WHERE o.status = 'On' ORDER BY payValue $orderx LIMIT $from, $max_results Quote Link to comment https://forums.phpfreaks.com/topic/141565-solved-php-mysql-order-by-field-from-other-table/#findComment-752445 Share on other sites More sharing options...
PeerFly Posted February 3, 2009 Author Share Posted February 3, 2009 When I try that code or blades code both result in no rows returned. Quote Link to comment https://forums.phpfreaks.com/topic/141565-solved-php-mysql-order-by-field-from-other-table/#findComment-753876 Share on other sites More sharing options...
PeerFly Posted February 4, 2009 Author Share Posted February 4, 2009 WOW! I finally got it resolved! SELECT o.offerid , o.name , o.incent , o.type , o.reporting , o.category , COALESCE(e.pay_value,o.pay_value) AS pay_value FROM offers AS o LEFT OUTER JOIN exceptions AS e ON e.offerid = o.offer_id WHERE o.status = 'On' ORDER BY pay_value Quote Link to comment https://forums.phpfreaks.com/topic/141565-solved-php-mysql-order-by-field-from-other-table/#findComment-754115 Share on other sites More sharing options...
fenway Posted February 6, 2009 Share Posted February 6, 2009 Interesting... COALESCE() simply returns the first NON-NULL value... but IFNULL() should have always given you a NON-NULL value... werid. Quote Link to comment https://forums.phpfreaks.com/topic/141565-solved-php-mysql-order-by-field-from-other-table/#findComment-755797 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.