Trying to display 2 tables data

倖福魔咒の 提交于 2019-12-22 13:47:53

问题


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|orderfrom
-------------------------------
bob|20-20|1|oakley
billy|15-20|2|Oakley, Maui
kim|20-20|3|Lux

I'm 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. I'm extremely new to joins so be easy when explaining.


回答1:


you must change your tables structure and you need to add a relation table with this values:

orders:

dist_id, patientacct_id PRIMARY_KEY(dist_id, patientacct_id)



回答2:


First of all, please make the table structure relational. Have a primary auto_increment value in each table and use the primary key from the Dlist foreign key to Patientacct. This way, a simple join would fetch the record you have wanted.

Regarding the display, what do you mean you don't want record twice. Since you are using join, you will get two records for Oakley with the patient Bob and Billy. That's what you wanted and it would be like that by a simple join too in a relational table.



来源:https://stackoverflow.com/questions/11183658/trying-to-display-2-tables-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!