Jump to content

php sql two tables joins help


thetylercox

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.