简体   繁体   English

左联接mysql多表ID并以JSON显示名称

[英]Left join mysql multi table id and display the name in JSON

I'm a new in angularjs and json . 我是angularjsjson angularjs I want to display the attribute name from id from one table (table A) that have relation to other table (table B) that contains id from table A. 我想从一个表(表A)的id显示属性name ,该属性name与包含表A的id另一表(表B)有关系。

here's table A: 这是表A:

---------------------
id_skill | name_skill
---------------------
1        | medical record
2        | rontgen
3        | thorax
---------------------

and table B: 和表B:

----------------------------------------
id_client | name_client | skill_client
----------------------------------------
1         | john        | 1,2
2         | smith       | 3
----------------------------------------

I want echoing/displaying data from table B, but in the skill_client column contains id(s) from table A. I want to display that column with name_skill from table A in json method and PHP . 我想回显/显示表B中的数据,但是在skill_client列中包含表A中的id(s) 。我想在表json方法和PHP表A中的name_skill显示该列。

so that I can get data in my page like this : 这样我就可以像这样在页面中获取数据:

------------------------
Name       |    Skills
------------------------
John       | medical record, rontgen
Smith      | thorax
------------------------

Can you help me? 你能帮助我吗?

You should really change the table structure and normalize your database. 您应该真正更改表结构并规范化数据库。 The best thing would be to create a new table to store the id_client and id_skill and remove the comma separated data from the 2nd table. 最好的办法是创建一个新表来存储id_clientid_skill并从第二个表中删除逗号分隔的数据。 However in your case you can try something as 但是,您可以尝试以下方法

select 
b.name_client, 
group_concat(a.name_skill) as skills 
from tableb b 
left join tablea a on find_in_set(a.id_skill,b.skill_client) > 0 
group by b.id_client ; 

Use explode(',', $myString); 使用explode(',',$ myString);

$myArray = explode(',', $myString);

This get an array of Strings from a String. 这将从字符串中获取字符串数组。

if string is: "medical record, rontgen" then you get a array whit lenght two: 如果字符串是: “病历,rontgen”,那么您会得到一个数组,长度为两个:

$myArray[0]= "medical record" $ myArray [0] =“病历”

$myArray[1]= "rontgen" $ myArray [1] =“ rontgen”

Then your use json_encode($yourData) to convert vars u objects in json string. 然后,您可以使用json_encode($ yourData)转换json字符串中的var u对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM