0

有一个时间与这个......我有以下简化表格

+---------+   +-------------+   +-------------+
| fields  |   | details     |   | customers   |
+---------+   +-------------+   +-------------+
| id      |   | customer_ID |   | customer_ID |
| caption |   | field_id    |   | progress    |
+---------+   | field_value |   +-------------+
              +-------------+

fields 表有多个“字段”,例如 first_name、last_name 等,它们都有一个 id,然后 details 中将包含适当的值,Customers 表处理“主”客户信息。

我想要做的是获取 details.field_value,fields.caption WHERE details.customer_id = customers_id AND customers.progress = X,然后按 last_name 的 id 对其进行排序。

我想让这个工作变得疯狂,所以我想我会在这里问一下,同时吃点午餐。

谢谢大家的帮助!

4

3 回答 3

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
于 2013-06-09T18:16:08.523 回答
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
于 2013-06-09T18:22:06.937 回答
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
于 2013-06-09T18:21:16.800 回答