sKunKbad Posted January 15, 2012 Share Posted January 15, 2012 I've got a query that may produce multiple results because of multiple tenants records, but I need to have only one result per unit: SELECT DISTINCT u.`unit-id`, u.`street-address`, u.`property-id-fk`, u.`suite-no`, t.`tenant-id` FROM unit_table u LEFT JOIN tenant_table t ON u.`unit-id` = t.`unit-id-fk` WHERE u.`status` = 'Inactive' AND u.`owner-id-fk` = ? This doesn't work unless I dont select tenant-id, but I need a tenant-id. If I put DISTINCT before t.`tenant-id` it produces an error. How can I have the query only return a single row for each unit AND tenant? Quote Link to comment https://forums.phpfreaks.com/topic/255036-distinct-for-two-fields-in-join/ Share on other sites More sharing options...
trq Posted January 15, 2012 Share Posted January 15, 2012 You want to GROUP BY, not DISTINCT. Quote Link to comment https://forums.phpfreaks.com/topic/255036-distinct-for-two-fields-in-join/#findComment-1307733 Share on other sites More sharing options...
fenway Posted January 16, 2012 Share Posted January 16, 2012 In fact, never use DISTINCT. Quote Link to comment https://forums.phpfreaks.com/topic/255036-distinct-for-two-fields-in-join/#findComment-1308211 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.