是否可以像我的例子中那样组合两个表?
表一:
bibid -- fieldid -- fied_data
400 ----- 10 ------- 107
400 ------ 5 -------- 1950
400 ------ 3 -------- USA
405 ------ 5 -------- 1997
405 ----- 10 -------- 90
405 ------ 3 -------- RUSSIA
表二:
bibid -------- name
400 --- Postman Always Rings Twice
405 --- Postman is a very good person
结果将是:
bibid --------- name --------------------------length ----------year ---------- country
400 -- Postman Always Rings Twice --------------107------------1950 ------------USA
405 -- Postman is a very good person -----------90-------------1997 ------------RUSSIA
答案 0 :(得分:0)
使用连接来获取数据。
SELECT one.bibid,
two.name,
one_10.fied_data as length,
one_5.fied_data as year,
one_3.fied_data as country
from table_two two
LEFT OUTER JOIN table_one one
ON one.bibid = two.bibid
LEFT OUTER JOIN table_one one_3
ON one.bibid = one_3.bibid
AND one_3.fieldid = 3
LEFT OUTER JOIN table_one one_5
ON one.bibid = one_5.bibid
AND one_5.fieldid = 5
LEFT OUTER JOIN table_one one_10
ON one.bibid = one_10.bibid
AND one_10.fieldid = 10
但是规范化数据会更好......
BTW:您的意思是field_data而不是fied_data吗?
答案 1 :(得分:0)
请尝试以下查询:
select o.bibid, t.name name,
(case when o.fieldid = 10 then o.fied_data end) length,
(case when o.fieldid = 5 then o.fied_data end) year,
(case when o.fieldid = 3 then o.fied_data end) countrty
from table_one o, table_two t
where o.bibid = t.bibid
group by o.bibidid, t.name
答案 2 :(得分:0)
看看group_cancat,我的mysql语法太生疏了,无法为你提供一个正确的例子。被困在T-SQL领域的时间太长了。这是一些伪的:
SELECT GROUP_CONCAT(fied_data) FROM T1 GROUP BY bibid
SELECT [Whatever] FROM T2 JOIN T1 ON ...
基本上你是连接* fied_data *的组,然后查询结果集并加入bibid。