0

我想在当前表引用的另一个表中的值之后排序。我的查询如下所示:

SELECT o._id,
       o.titel,
       o.beschreibung
FROM   `objekt` AS o,
       `objekt_einzel` AS oe,
       `objekt_einzel_immobilie` AS oei,
       `objekt_art` AS oa,
       `verortung` AS v
       #here
       ,`person` AS p,
       `person_bauträger` AS pb
       #end
WHERE  o._id = oe.objekt_id
       AND oe._id = oei.objekt_einzel_id
       AND oa._id = o.objekt_art_id
       AND o.ort_id = v._id
       #here
       AND oe.bauträger_id = pb._id
       AND pb.person_id = p._id
       #end
       AND ( oei.justimmo_objekt_id = "0"
              OR oei.justimmo_objekt_id IS NULL
              OR oei.justimmo_objekt_id = "" )
#here
ORDER  BY p.firmenbezeichnung ASC

查询工作正常,但它只显示oe.bauträger_id设置的值。我也想要null价值观。所以我需要一个左连接。我尝试了不同的事情,但我只收到类似的消息,unknown column或者我得到了太多的结果。

我试图将其简化为:

SELECT o._id,
       o.titel,
       o.beschreibung
FROM   `objekt` AS o,
       `objekt_einzel` AS oe,
        (SELECT oe.bauträger_id
        FROM objekt o, objekt_einzel oe, objekt_einzel_immobilie oei
        WHERE o._id = oe.objekt_id AND oe._id = oei.objekt_einzel_id) AS menge1
LEFT JOIN
        (SELECT pb._id AS bauträger_id
        FROM person p, person_bauträger pb
        WHERE p._id = pb.person_id) AS menge2
ON menge1.bauträger_id = menge2.bauträger_id
WHERE o._id = oe.objekt_id AND oe.bauträger_id = menge1.bauträger_id

但在这里我得到了一个太大的结果集。我不知道如何更好地解释这一点。数据集太大,无法创建示例。我希望你明白我的意思。

4

1 回答 1

1
SELECT o._id,
       o.titel,
       o.beschreibung
FROM   `objekt` AS o
       JOIN `objekt_einzel` AS oe ON o._id = oe.objekt_id
       JOIN `objekt_einzel_immobilie` AS oei ON oe._id = oei.objekt_einzel_id
       JOIN `objekt_art` AS oa ON o.objekt_art_id = oa._id
       JOIN `verortung` AS v ON o.ort_id = v._id
       LEFT JOIN `person_bauträger` AS pb ON oe.bauträger_id = pb._id
       LEFT JOIN `person` AS p ON pb.person_id = p._id
WHERE  oei.justimmo_objekt_id = "0"
       OR oei.justimmo_objekt_id IS NULL
       OR oei.justimmo_objekt_id = ""
ORDER  BY p.firmenbezeichnung ASC

第二次尝试应该可以工作,因为它只是使用 JOIN 语法和 LEFT JOIN 重写的原始代码。

于 2013-10-30T08:22:31.280 回答