thetylercox Posted June 25, 2012 Share Posted June 25, 2012 I have 2 tables Table name Dist. NAME|Phone|ID ---------------- Oakley|555-555|1 Maui|666-666|2 lux|777-7777|3 Table name Patientacct. Name|prescription|id|dist ------------------------------- bob|20-20|1|oakley billy|15-20|2|Oakley, Maui kim|20-20|3|Lux Im looking for a display like Oakley -------------------- Bob Billy Maui ------------------ Billy Lux -------------- kim Trials so far SELECT * FROM `dist` JOIN `patientacct` ON patientacct.dist LIKE CONCAT('%', `dist`.name ,'%') GROUP BY `dist`.name This showed only 1 dist if i drop the group by example:SELECT * FROM `dist` JOIN `patientacct` ON patientacct.dist LIKE CONCAT('%', `dist`.name ,'%') i get the record twice so what I need is somewhere between the two. Im extremely new to joins so be easy when explaining. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/264720-php-sql-two-tables-joins-help/ Share on other sites More sharing options...
Drummin Posted June 25, 2012 Share Posted June 25, 2012 Have you tried LEFT JOIN? Quote Link to comment https://forums.phpfreaks.com/topic/264720-php-sql-two-tables-joins-help/#findComment-1356750 Share on other sites More sharing options...
thetylercox Posted June 25, 2012 Author Share Posted June 25, 2012 as i said im entirely new to the idea of joins. I have not tried any other join than the one i listed. I have been trying to read but its a little difficult to wrap my head around at the moment. How would a left join differ? Whats the querry sintax u would suggest? Quote Link to comment https://forums.phpfreaks.com/topic/264720-php-sql-two-tables-joins-help/#findComment-1356826 Share on other sites More sharing options...
thetylercox Posted June 25, 2012 Author Share Posted June 25, 2012 how about an easier question how could i link these tables "relational" when one record can be tied to one or more other records. As above oakley is tied to bob and billy, Quote Link to comment https://forums.phpfreaks.com/topic/264720-php-sql-two-tables-joins-help/#findComment-1356835 Share on other sites More sharing options...
thetylercox Posted June 25, 2012 Author Share Posted June 25, 2012 how to get from this to this is the question! Quote Link to comment https://forums.phpfreaks.com/topic/264720-php-sql-two-tables-joins-help/#findComment-1356853 Share on other sites More sharing options...
Drummin Posted June 25, 2012 Share Posted June 25, 2012 You should move away from using a name to tie these records together. I assume that id is a primary key in the Dist table. In all other tables that would tie with this table, there should be a column that holds this id like dist_id. $sql="SELECT d.name, d.phone, p.pame, p.prescription, p.id FROM `dist` AS d LEFT JOIN `patientacct` AS p ON d.id = p.dist_id GROUP BY d.name"; This should pull up each district and then list any patient account data that has the dist_id, then the next record etc. Quote Link to comment https://forums.phpfreaks.com/topic/264720-php-sql-two-tables-joins-help/#findComment-1356868 Share on other sites More sharing options...
Drummin Posted June 25, 2012 Share Posted June 25, 2012 I wanted to also point you to this post regarding DB structure. This is an important concept to understand when starting out. http://forums.phpfreaks.com/index.php?topic=361424.msg1710272#msg1710272 Quote Link to comment https://forums.phpfreaks.com/topic/264720-php-sql-two-tables-joins-help/#findComment-1356893 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.