SQL 合并表的查询 (n:n)
SQL query to combine tables (n:n)
我有这些 table:
类别:
- id
- 姓名
KIBE_TRAEGER:
- id
- 姓名
国家:
- id
- region_id
- country_id
- county_code
- county_de
地区:
- id
- 国家
- region_de
联系人:
- 编号
- 名字
- 姓氏
- 公司
- 邮政编码
- 城市
- 地区 --> =regions.id --> 应该显示 regions.region_de
- 县 --> =counties.id --> 应该显示 counties.county_de
- tel1
- 电话 2
- 手机
- 传真
- 电子邮件 1
- 电子邮件 2
- 首页
- 类别 --> =categories.id BUT 在 table "contacts" 中是这样写的
与;catnr; (例如,一个条目可以有多个条目:
";6;;7;;8;;16;") --> 应该显示 categories.name
- 注释
- 活跃
- pernr
KIBE_CONTACTS:
- id
- 营业时间
- 成本
- 组
- 残障人士
- 自由空间
- traeger --> =kibe_traeger.id 与类别相同;traegerId; (e.g.";1;;5;") --> 应该显示 kibe_traeger.name
我想将它们全部合并以将其导出到 csv。
顺便说一句。到目前为止,我直接在 phpmyadmin 中使用左连接和内连接尝试了我糟糕的查询,但我在 SQL 方面很差,不会再学习了:P ...
到目前为止我的代码(根据 Matt Cremeen 的回答编辑):
工作:
SELECT
contacts.id, contacts.firstname, contacts.surname, contacts.company,
contacts.zipcode, contacts.city, contacts.country, contacts.region,
contacts.county, contacts.tel1, contacts.tel2, contacts.mobile,
contacts.fax, contacts.email1, contacts.email2, contacts.homepage,
contacts.active, contacts.pernr, kibe_contacts.id as contacts_id,
kibe_contacts.openinghours, kibe_contacts.costs, kibe_contacts.groups,
kibe_contacts.handicapplaces, kibe_contacts.freeplaces,
kibe_contacts.traeger, kibe_traeger.id as traeger_id, kibe_traeger.name,
counties.id as counties_id, counties.region_id as region_id,
counties.country_id as country_id, counties.county_code, counties.county_de,
regions.id as regions_id, regions.country as regions_country, regions.region_de
FROM
contacts
INNER JOIN
kibe_contacts ON contacts.id = kibe_contacts.contact_id
INNER JOIN
regions ON contacts.region = regions.id
INNER JOIN
counties ON contacts.county = counties.id
不 工作:
INNER JOIN
kibe_traeger ON kibe_contacts.traeger = kibe_traeger.id
INNER JOIN
categories ON contacts.categories = categories.id;
我得到一个 "Null-Result" 我想我知道为什么我会收到这个错误:因为在 kibe_contacts.traeger
和 contacts.categories
列中有多个值被分隔;
例如一个条目可以有多个条目:;6;;7;;8;;16;
但在 categories.id 中总是只有一个 ID,例如4.
请问有解决办法吗?
上次编辑: 自己解决了第二个问题:我导出列表并用搜索和替换完成。
再次感谢各位帮手!
尝试使用MySQL的GROUP_CONCAT
功能。这是我的样本 SQL
我有 2 张桌子,clientes ('Id' int, 'nome' varchar(50) )
clientes
1 Rafael
2 Jony
3 Smith
和clientes_devices ('Id' int, 'id_cliente' int, 'device' varchar(50) )
clientes_devices
1 1 AsusZenFone
2 1 MotorolaG
3 2 WindowsPhone
这里是select命令
select c.nome, GROUP_CONCAT(DISTINCT d.device SEPARATOR ',') AS Device
from clientes c
join clientes_dispositivos d on (c.id = d.id_cliente)
where c.id = 1
这是输出
没有这个功能,我得到了这个
假设 id
字段对应于每个 table,您将要加入该字段上的每个 table。我不会在这里写所有的代码,但它会像这样开始
SELECT contacts.id, contacts.firstname, contacts.surname, ...
FROM contacts
INNER JOIN
kibe_contacts
ON contacts.id = kibe_contacts.id
INNER JOIN
regions
on contacts.region_id = regions.id
...
我有这些 table:
类别:
- id
- 姓名
KIBE_TRAEGER:
- id
- 姓名
国家:
- id
- region_id
- country_id
- county_code
- county_de
地区:
- id
- 国家
- region_de
联系人:
- 编号
- 名字
- 姓氏
- 公司
- 邮政编码
- 城市
- 地区 --> =regions.id --> 应该显示 regions.region_de
- 县 --> =counties.id --> 应该显示 counties.county_de
- tel1
- 电话 2
- 手机
- 传真
- 电子邮件 1
- 电子邮件 2
- 首页
- 类别 --> =categories.id BUT 在 table "contacts" 中是这样写的 与;catnr; (例如,一个条目可以有多个条目: ";6;;7;;8;;16;") --> 应该显示 categories.name
- 注释
- 活跃
- pernr
KIBE_CONTACTS:
- id
- 营业时间
- 成本
- 组
- 残障人士
- 自由空间
- traeger --> =kibe_traeger.id 与类别相同;traegerId; (e.g.";1;;5;") --> 应该显示 kibe_traeger.name
我想将它们全部合并以将其导出到 csv。
顺便说一句。到目前为止,我直接在 phpmyadmin 中使用左连接和内连接尝试了我糟糕的查询,但我在 SQL 方面很差,不会再学习了:P ...
到目前为止我的代码(根据 Matt Cremeen 的回答编辑):
工作:
SELECT
contacts.id, contacts.firstname, contacts.surname, contacts.company,
contacts.zipcode, contacts.city, contacts.country, contacts.region,
contacts.county, contacts.tel1, contacts.tel2, contacts.mobile,
contacts.fax, contacts.email1, contacts.email2, contacts.homepage,
contacts.active, contacts.pernr, kibe_contacts.id as contacts_id,
kibe_contacts.openinghours, kibe_contacts.costs, kibe_contacts.groups,
kibe_contacts.handicapplaces, kibe_contacts.freeplaces,
kibe_contacts.traeger, kibe_traeger.id as traeger_id, kibe_traeger.name,
counties.id as counties_id, counties.region_id as region_id,
counties.country_id as country_id, counties.county_code, counties.county_de,
regions.id as regions_id, regions.country as regions_country, regions.region_de
FROM
contacts
INNER JOIN
kibe_contacts ON contacts.id = kibe_contacts.contact_id
INNER JOIN
regions ON contacts.region = regions.id
INNER JOIN
counties ON contacts.county = counties.id
不 工作:
INNER JOIN
kibe_traeger ON kibe_contacts.traeger = kibe_traeger.id
INNER JOIN
categories ON contacts.categories = categories.id;
我得到一个 "Null-Result" 我想我知道为什么我会收到这个错误:因为在 kibe_contacts.traeger
和 contacts.categories
列中有多个值被分隔;
例如一个条目可以有多个条目:;6;;7;;8;;16;
但在 categories.id 中总是只有一个 ID,例如4.
请问有解决办法吗?
上次编辑: 自己解决了第二个问题:我导出列表并用搜索和替换完成。
再次感谢各位帮手!
尝试使用MySQL的GROUP_CONCAT
功能。这是我的样本 SQL
我有 2 张桌子,clientes ('Id' int, 'nome' varchar(50) )
clientes
1 Rafael
2 Jony
3 Smith
和clientes_devices ('Id' int, 'id_cliente' int, 'device' varchar(50) )
clientes_devices
1 1 AsusZenFone
2 1 MotorolaG
3 2 WindowsPhone
这里是select命令
select c.nome, GROUP_CONCAT(DISTINCT d.device SEPARATOR ',') AS Device
from clientes c
join clientes_dispositivos d on (c.id = d.id_cliente)
where c.id = 1
这是输出
没有这个功能,我得到了这个
假设 id
字段对应于每个 table,您将要加入该字段上的每个 table。我不会在这里写所有的代码,但它会像这样开始
SELECT contacts.id, contacts.firstname, contacts.surname, ...
FROM contacts
INNER JOIN
kibe_contacts
ON contacts.id = kibe_contacts.id
INNER JOIN
regions
on contacts.region_id = regions.id
...