1

我有这个查询。还请查找以下表架构

http://sqlfiddle.com/#!2/36d20/14

SELECT
  ca1.*,
  ca2.*
FROM Contact_Address AS ca1
  JOIN Contact_Address AS ca2
    ON ca1.ca_contact_id = ca2.ca_contact_id
WHERE ca1.ca_contact_id = 1
    AND ca1.ca_type = 'PRI'
    AND ca2.ca_type = 'SEC'

我必须将包含在同一个表中的两行合并为单行。它们包含类型“PRI”、“SEC”。但条件是对于一个用户它将包含两行,而对于另一个用户它将只包含一个。

请帮忙,谢谢

4

1 回答 1

0
SELECT
  ca1.ca_contact_id as ci1,ca1.ca_type as type1,
  ca2.ca_contact_id as ci2, ca2.ca_type as type2
FROM Contact_Address AS ca1
  JOIN Contact_Address AS ca2
    ON ca1.ca_contact_id = ca2.ca_contact_id
WHERE ca1.ca_contact_id = 1
    AND ca1.ca_type = 'PRI'
    AND ca2.ca_type = 'SEC'

SqlFiddle

您必须指定您的 col 名称

于 2013-06-26T06:13:50.650 回答