有一次与这一次....我有以下简化表
+---------+ +-------------+ +-------------+
| fields | | details | | customers |
+---------+ +-------------+ +-------------+
| id | | customer_ID | | customer_ID |
| caption | | field_id | | progress |
+---------+ | field_value | +-------------+
+-------------+
字段表有多个“字段”,例如first_name,last_name等,它们上面有id,然后详细信息中包含相应的值,Customers表处理“主”客户信息。
我要做的是获取details.field_value,fields.caption WHERE details.customer_id = customers_id AND customers.progress = X然后用last_name的id对它进行ORDER。
我为了让这个工作变得疯狂,所以我想在这里问一下午餐。
谢谢大家的帮助!
答案 0 :(得分:1)
我认为您可以使用INNER JOIN
查询
SELECT c.id, c.caption
FROM customers a
INNER JOIN details b
ON a.customers_ID = b.customers_ID
INNER JOIN fields c
ON b.field_id = c.id
WHERE a.progress = ?
ORDER BY b.last_name ASC
答案 1 :(得分:0)
试试这个:
SELECT d.field_value, f.caption
FROM (details d INNER JOIN fields f ON d.field_id = f.id)
INNER JOIN customers c ON d.customer_ID = c.customer_ID
WHERE c.progress = 'X'
ORDER BY f.id
答案 2 :(得分:0)
一种方式:
select c.customer_ID,
group_concat(concat(f.caption,':',d.field_value)) Data_Fields,
max(case f.caption when 'last_name' then d.field_value end) last_name
from customers c
join details d on c.customer_ID = d.c.customer_ID
join fields f on d.field_id = f.id
where c.progress=?
group by c.customer_ID
order by last_name