我想根据where条件从两个不同的数据库中获取数据 我在其中提取用户名,地址,密码,街道地址和语言。现在问题是我为同一个用户提供了多行语言,如下所示。
pid field_languages_spoken_value
1 English
1 Spanish
2 English
现在我可以将它们显示为"英语,西班牙语"。
我的查询代码:
SELECT x.field_name_value,
x.field_street_address_value,
x.field_zip_code_value,
x.field_street_address_value,
x.field_zip_code_value,
x.field_test_tid,
x.field_test_tid,
x.field_county_value,
x.field_county_value,
x.field_name_value,
x.field_address_value,
x.field_county_value,
x.mail,
x.field_name_value,
x.field_languages_spoken_value,
x.field_languages_spoken_value,
x.field_address_value,
x.field_county_value,
x.mail
FROM (SELECT a.field_name_value,
b.field_address_value,
c.field_county_value,
d.field_test_tid,
e.mail,
k.field_street_address_value,
l.field_zip_code_value,
p.uid,
Group_concat(Distinct lang.field_languages_spoken_value)
FROM db1.users e
INNER JOIN db1.profile AS p
ON p.uid = e.uid
INNER JOIN db1.field_data_field_languages_spoken AS lang
ON lang.entity_id = p.pid
INNER JOIN db1.field_data_field_name AS a
ON a.entity_id = p.pid
INNER JOIN db1.field_data_field_address AS b
ON b.entity_id = p.pid
INNER JOIN db1.field_data_field_county AS c
ON c.entity_id = p.pid
INNER JOIN db1.field_data_field_street_address AS k
ON k.entity_id = p.pid
INNER JOIN db1.field_data_field_zip_code AS l
ON l.entity_id = p.pid
INNER JOIN db1.field_data_field_test AS d
ON d.entity_id = p.pid
UNION ALL
SELECT f.field_name_value,
g.field_address_value,
h.field_county_value,
i.field_test_tid,
j.mail,
m.field_street_address_value,
n.field_zip_code_value,
q.uid,
Group_concat(Distinct lange.field_languages_spoken_value)
FROM db2.users j
INNER JOIN db2.profile AS q
ON q.uid = j.uid
INNER JOIN db2.field_data_field_languages_spoken AS lange
ON lange.entity_id = q.pid
INNER JOIN db2.field_data_field_name AS f
ON f.entity_id = q.pid
INNER JOIN db2.field_data_field_address AS g
ON g.entity_id = q.pid
INNER JOIN db2.field_data_field_county AS h
ON h.entity_id = q.pid
INNER JOIN db2.field_data_field_street_address AS m
ON m.entity_id = q.pid
INNER JOIN db2.field_data_field_zip_code AS n
ON n.entity_id = q.pid
INNER JOIN db2.field_data_field_test AS i
ON i.entity_id = q.pid) AS x
WHERE x.field_county_value LIKE :city
AND x.field_test_tid LIKE :speciality
我该如何处理这个问题。
答案 0 :(得分:0)
尝试在group concat中使用Separator
GROUP_CONCAT(lange.field_languages_spoken_value SEPARATOR ',')
按照uid分组
希望结果与您的期望相同:)
[EDITED]: 试试这个查询我已修改它希望那里没有错误并按预期结果:)
SELECT x.field_name_value,
x.field_street_address_value,
x.field_zip_code_value,
x.field_street_address_value,
x.field_zip_code_value,
x.field_test_tid,
x.field_test_tid,
x.field_county_value,
x.field_county_value,
x.field_name_value,
x.field_address_value,
x.field_county_value,
x.mail,
x.field_name_value,
x.field_languages_spoken_value,
x.field_languages_spoken_value,
x.field_address_value,
x.field_county_value,
x.mail
FROM (SELECT a.field_name_value,
b.field_address_value,
c.field_county_value,
d.field_test_tid,
e.mail,
k.field_street_address_value,
l.field_zip_code_value,
p.uid,
lange.field_languages_spoken_value
FROM db1.users e
INNER JOIN db1.profile AS p
ON p.uid = e.uid
INNER JOIN (SELECT lange1.entity_id,GROUP_CONCAT(lange1.field_languages_spoken_value SEPARATOR ',') as `field_languages_spoken_value` FROM db1.field_data_field_languages_spoken lange1 GROUP BY lange1.entity_id) AS lange
ON lang.entity_id = p.pid
INNER JOIN db1.field_data_field_name AS a
ON a.entity_id = p.pid
INNER JOIN db1.field_data_field_address AS b
ON b.entity_id = p.pid
INNER JOIN db1.field_data_field_county AS c
ON c.entity_id = p.pid
INNER JOIN db1.field_data_field_street_address AS k
ON k.entity_id = p.pid
INNER JOIN db1.field_data_field_zip_code AS l
ON l.entity_id = p.pid
INNER JOIN db1.field_data_field_test AS d
ON d.entity_id = p.pid
UNION ALL
SELECT f.field_name_value,
g.field_address_value,
h.field_county_value,
i.field_test_tid,
j.mail,
m.field_street_address_value,
n.field_zip_code_value,
q.uid,
lange.field_languages_spoken_value
FROM db2.users j
INNER JOIN db2.profile AS q
ON q.uid = j.uid
INNER JOIN (SELECT lange2.entity_id,GROUP_CONCAT(lange2.field_languages_spoken_value SEPARATOR ',') as `field_languages_spoken_value` FROM db2.field_data_field_languages_spoken lange2 GROUP BY lange2.entity_id) AS lange
ON lange.entity_id = q.pid
INNER JOIN db2.field_data_field_name AS f
ON f.entity_id = q.pid
INNER JOIN db2.field_data_field_address AS g
ON g.entity_id = q.pid
INNER JOIN db2.field_data_field_county AS h
ON h.entity_id = q.pid
INNER JOIN db2.field_data_field_street_address AS m
ON m.entity_id = q.pid
INNER JOIN db2.field_data_field_zip_code AS n
ON n.entity_id = q.pid
INNER JOIN db2.field_data_field_test AS i
ON i.entity_id = q.pid) AS x
WHERE x.field_county_value LIKE :city
AND x.field_test_tid LIKE :speciality
答案 1 :(得分:0)
试试这个。
SELECT x.field_name_value,
x.field_street_address_value,
x.field_zip_code_value,
x.field_street_address_value,
x.field_zip_code_value,
x.field_test_tid,
x.field_test_tid,
x.field_county_value,
x.field_county_value,
x.field_name_value,
x.field_address_value,
x.field_county_value,
x.mail,
x.field_name_value,
x.field_languages_spoken_value,
x.field_languages_spoken_value,
x.field_address_value,
x.field_county_value,
x.mail
FROM (SELECT a.field_name_value,
b.field_address_value,
c.field_county_value,
d.field_test_tid,
e.mail,
k.field_street_address_value,
l.field_zip_code_value,
p.uid,
Group_concat(distinct lang.field_languages_spoken_value) as field_languages_spoken_value
FROM db1.users e
INNER JOIN db1.profile AS p
ON p.uid = e.uid
INNER JOIN db1.field_data_field_languages_spoken AS lang
ON lang.entity_id = p.pid
INNER JOIN db1.field_data_field_name AS a
ON a.entity_id = p.pid
INNER JOIN db1.field_data_field_address AS b
ON b.entity_id = p.pid
INNER JOIN db1.field_data_field_county AS c
ON c.entity_id = p.pid
INNER JOIN db1.field_data_field_street_address AS k
ON k.entity_id = p.pid
INNER JOIN db1.field_data_field_zip_code AS l
ON l.entity_id = p.pid
INNER JOIN db1.field_data_field_test AS d
ON d.entity_id = p.pid
UNION ALL
SELECT f.field_name_value,
g.field_address_value,
h.field_county_value,
i.field_test_tid,
j.mail,
m.field_street_address_value,
n.field_zip_code_value,
q.uid,
Group_concat(distinct lange.field_languages_spoken_value) as field_languages_spoken_value
FROM db2.users j
INNER JOIN db2.profile AS q
ON q.uid = j.uid
INNER JOIN db2.field_data_field_languages_spoken AS lange
ON lange.entity_id = q.pid
INNER JOIN db2.field_data_field_name AS f
ON f.entity_id = q.pid
INNER JOIN db2.field_data_field_address AS g
ON g.entity_id = q.pid
INNER JOIN db2.field_data_field_county AS h
ON h.entity_id = q.pid
INNER JOIN db2.field_data_field_street_address AS m
ON m.entity_id = q.pid
INNER JOIN db2.field_data_field_zip_code AS n
ON n.entity_id = q.pid
INNER JOIN db2.field_data_field_test AS i
ON i.entity_id = q.pid) AS x
WHERE x.field_county_value LIKE :city
AND x.field_test_tid LIKE :speciality