获取相同行ID的查询问题但行ID不同

时间:2017-11-08 05:35:29

标签: mysql

我已经运行了以下查询以获取不同的lineId'但它显示的是同一行' s我在查询中找不到问题所在。请检查以下查询和

请建议我解决问题。

查询:

SELECT
    item.id AS itemId,
    ol.id AS lineId,
    l.description AS order_description,
    l.type_id AS itemType,
    l.quantity,
    l.amount AS amount
FROM
    invoice_line l, purchase_order po, order_line ol,
    order_process op, item,invoice i,item_type it,item_type_map itm
WHERE
    i.id = l.invoice_id AND
    op.invoice_id = l.invoice_id AND
    op.order_id = po.id AND
    po.id = ol.order_id AND
    l.invoice_id = 10325 AND
    ol.item_id = itm.item_id AND 
    itm.type_id = it.id AND
    l.item_id = item.id AND
    item.id = itm.item_id AND
    it.description <> 'Provisionable' AND
    it.description <> 'Usage' AND
    it.description <> 'Refund' AND
    NOT l.description = "voice call" AND
    it.id = 280500
GROUP BY
    it.id,
    l.description,
    l.id;

enter image description here

1 个答案:

答案 0 :(得分:0)

我不清楚你想在这里做什么,但我会先重写查询以使其更易理解,我会通过一次添加连接和条件来重建,这样我就能理解每一步发生了什么

SELECT
    item.id AS itemId,
    ol.id AS lineId,
    l.description AS order_description,
    l.type_id AS itemType,
    l.quantity,
    l.amount AS amount
FROM        item
join        item_type it item.id = itm.item_id
join        item_type_map itm itm.type_id = it.id
left join       order_process op op.order_id = po.id
left join       order_line ol ol.item_id = itm.item_id
left join       purchase_order po po.id = ol.order_id
left join       invoice i op.invoice_id = l.invoice_id
left join       invoice_line l l.item_id = item.id

WHERE
    i.id = l.invoice_id AND #????
    l.invoice_id = 10325 AND
    it.description <> 'Provisionable' AND
    it.description <> 'Usage' AND
    it.description <> 'Refund' AND
    l.description <> 'voice call' AND
    it.id = 280500

我假设item,item_type和item_type_map之间存在1对1的关系。但在那之后我不知道与其他桌子的关系。如果您可以将所有这些表中的样本数据作为文本添加到您的问题中,将会有所帮助。我不明白 NOT l.description =&#34;语音通话&#34; 因此我更改了它并删除了小组,因为我不知道你想要什么结果。