我想要做的是查询一个表,其中包含关于某人的自定义数据并将其存入字段,但是我将每个单独的字段都记录在我的返回中。MySQL - 将记录合并到字段中
我使用的是当前语句是
SELECT s.fname, s.lname, s.email, s.mobile, s.country, cf.name, ca.value FROM
standard AS s
INNER JOIN people AS p ON
(s.pid = p.pid)
INNER JOIN custom_answers AS ca ON
(ca.pid = p.pid)
INNER JOIN custom_fields AS cf ON
(cf.fieldid = ca.fieldid)
WHERE p.acctid = 'xxxxxxxxxx'
这给出22000行的一个结果,而我只是在寻找900行。
数据输出的一个例子是
fname | lname | email | mobile | country | name | value
tom | smith | [email protected] | 0412 | AU | state | Vic
tom | smith | [email protected] | 0412 | AU | position | Dept Head
tom | smith | [email protected] | 0412 | AU | guest | John Smith
mick | jones | [email protected] | 0411 | AU | postnom | AOC
mick | jones | [email protected] | 0411 | AU | state | NSW
mick | jones | [email protected] | 0411 | AU | postcode | 2000
而我会它想输出是
fname | lname | email | mobile | country | state | position | guest | postnom | postcode
tom | smith | [email protected] | 0412 | AU | Vic | Dept Head | John Smith | null | null
mick | jones | [email protected] | 0411 | AU | NSW | null | null | AOC | 2000
什么可能会或可能不会导致复杂的是,自定义字段的量ISN”每个人都一样。有些可能只有少数或自定义字段,但有些可能有30个以上。
答案需要提高以使用外部连接,因为并不是每个用户都表示每个自定义字段。 – dbenham
@dbenham,谢谢。连接仍然让我有些不解。 – JohnP