coupe-r Posted September 19, 2011 Share Posted September 19, 2011 Hi All, I'm joining my passes table with my property table so when I'm on the passes page, I can see which properties are assigned to the list of passes. However, I have 7 pass records (101 - 107), but my query pulls 15 records (103 - 107) 3 times... Now if I delete a property record, it then pulls those records 2 times or if I add a 4th, it pulls them 4 times each.. Here is the query: $sql = "SELECT p.pass_id, p.property_id, p.number, p.descrip, pr.address1, pr.address2 "; $sql .= "FROM passes p LEFT JOIN property pr ON p.client_id = pr.client_id "; $sql .= "WHERE p.client_id = '".$_SESSION['client_id']."' AND pr.client_id = '".$_SESSION['client_id']."' AND p.deleted_on IS NULL "; $sql .= "ORDER BY p.pass_id DESC LIMIT $offset, $rowsperpage"; Link to comment https://forums.phpfreaks.com/topic/247427-query-help-giving-me-more-results-than-exist/ Share on other sites More sharing options...
AyKay47 Posted September 19, 2011 Share Posted September 19, 2011 from your query, i can't see why it would be giving you multiple results.. can you post the relevant code Link to comment https://forums.phpfreaks.com/topic/247427-query-help-giving-me-more-results-than-exist/#findComment-1270628 Share on other sites More sharing options...
coupe-r Posted September 19, 2011 Author Share Posted September 19, 2011 Thanks for reading, however, the relevant code shouldnt matter. This query pulls 7 records, which is correct. SELECT COUNT(pass_id) FROM passes WHERE client_id = '".$_SESSION['client_id']."' AND deleted_on IS NULL As soon as I join it with the property table, it grabs multiple, because there are multiple properties. Each table has client_id, so I'm assuming its finding a property and listing the passes for each instance? Link to comment https://forums.phpfreaks.com/topic/247427-query-help-giving-me-more-results-than-exist/#findComment-1270636 Share on other sites More sharing options...
AyKay47 Posted September 19, 2011 Share Posted September 19, 2011 the WHERE clause will determine how many rows are actually grabbed.. the ON clause will only determine how the tables are joined.. that being said.. try this $sql = "SELECT p.pass_id, p.property_id, p.number, p.descrip, pr.address1, pr.address2 "; $sql .= "FROM passes p LEFT JOIN property pr ON p.client_id = pr.client_id "; $sql .= "WHERE p.client_id = '".$_SESSION['client_id']."' AND p.deleted_on IS NULL "; $sql .= "ORDER BY p.pass_id DESC LIMIT $offset, $rowsperpage"; Link to comment https://forums.phpfreaks.com/topic/247427-query-help-giving-me-more-results-than-exist/#findComment-1270639 Share on other sites More sharing options...
coupe-r Posted September 19, 2011 Author Share Posted September 19, 2011 Ahh, you got it, sorta... Removing pr.client_id from the WHERE clause and changing what the tables are joined on fixed it. client_id = client_id should have been property_id = property_id.. $sql = "SELECT p.pass_id, p.property_id, p.number, p.descrip, pr.address1, pr.address2 "; $sql .= "FROM passes p LEFT JOIN property pr ON p.property_id = pr.property_id "; $sql .= "WHERE p.client_id = '".$_SESSION['client_id']."' AND p.deleted_on IS NULL "; $sql .= "ORDER BY p.pass_id DESC LIMIT $offset, $rowsperpage"; Thanks so much Link to comment https://forums.phpfreaks.com/topic/247427-query-help-giving-me-more-results-than-exist/#findComment-1270643 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.