Php 有没有办法根据MySQL中的一个案例动态定义一个操作符?

Php 有没有办法根据MySQL中的一个案例动态定义一个操作符?,php,mysql,join,left-join,Php,Mysql,Join,Left Join,我有一个查询,它自己连接起来,为每个唯一列表获取最新或最旧的订单记录 SELECT lists.ordered, cnt1.* FROM cnt_lists as cnt1 LEFT JOIN lists on cnt1.list_id = lists.id LEFT JOIN cnt_lists as cnt2 on (cnt1.li

我有一个查询,它自己连接起来,为每个唯一列表获取最新或最旧的订单记录

SELECT
    lists.ordered,
    cnt1.*
FROM 
    cnt_lists as cnt1
    LEFT JOIN 
        lists
        on 
            cnt1.list_id = lists.id
    LEFT JOIN 
        cnt_lists as cnt2
        on 
            (cnt1.list_id = cnt2.list_id AND cnt1.id < cnt2.id)

WHERE
    cnt2.id is null and cnt1.list_id in ('3176', '3295', '3296') and cnt1.listable_type = 'Movie';
如何为每个列表同时提取最高顺序和最低顺序,我可以确定在PHP端使用这两个记录中的哪一个


我只是在寻找一些想法,因为我试图避免N+1查询问题,即必须单独查询每个列表。

依赖动态查询是很麻烦的。最好将该运算符拉入查询的WHERE部分,并将其与OR配对:

SELECT
    lists.ordered,
    cnt1.*
FROM 
    cnt_lists as cnt1
    LEFT JOIN 
        lists
        on 
            cnt1.list_id = lists.id
    LEFT JOIN 
        cnt_lists as cnt2
        on 
            (cnt1.list_id = cnt2.list_id)

WHERE
    cnt2.id is null and cnt1.list_id in ('3176', '3295', '3296') 
    and cnt1.listable_type = 'Movie'
    AND (
       cnt2.id IS NULL /* Including this here because you have a LEFT JOIN */
       (lists.ordered = 1 AND cnt1.id < cnt2.id) OR 
       (lists.ordered = 0 AND cnt1.id > cnt2.id)
    );
您的另一个选择是直接将该逻辑放入连接逻辑。这可能更容易阅读

SELECT
    lists.ordered,
    cnt1.*
FROM 
    cnt_lists as cnt1
    LEFT JOIN 
        lists
        on 
            cnt1.list_id = lists.id
    LEFT JOIN 
        cnt_lists as cnt2
        on 
            (cnt1.list_id = cnt2.list_id AND (
       (lists.ordered = 1 AND cnt1.id < cnt2.id) OR 
       (lists.ordered = 0 AND cnt1.id > cnt2.id)
    ))

WHERE
    cnt2.id is null and cnt1.list_id in ('3176', '3295', '3296') 
    and cnt1.listable_type = 'Movie'
    ;

您确实可以选择包含依赖动态查询是很麻烦的。最好将该运算符拉入查询的WHERE部分,并将其与OR配对:

SELECT
    lists.ordered,
    cnt1.*
FROM 
    cnt_lists as cnt1
    LEFT JOIN 
        lists
        on 
            cnt1.list_id = lists.id
    LEFT JOIN 
        cnt_lists as cnt2
        on 
            (cnt1.list_id = cnt2.list_id)

WHERE
    cnt2.id is null and cnt1.list_id in ('3176', '3295', '3296') 
    and cnt1.listable_type = 'Movie'
    AND (
       cnt2.id IS NULL /* Including this here because you have a LEFT JOIN */
       (lists.ordered = 1 AND cnt1.id < cnt2.id) OR 
       (lists.ordered = 0 AND cnt1.id > cnt2.id)
    );
您的另一个选择是直接将该逻辑放入连接逻辑。这可能更容易阅读

SELECT
    lists.ordered,
    cnt1.*
FROM 
    cnt_lists as cnt1
    LEFT JOIN 
        lists
        on 
            cnt1.list_id = lists.id
    LEFT JOIN 
        cnt_lists as cnt2
        on 
            (cnt1.list_id = cnt2.list_id AND (
       (lists.ordered = 1 AND cnt1.id < cnt2.id) OR 
       (lists.ordered = 0 AND cnt1.id > cnt2.id)
    ))

WHERE
    cnt2.id is null and cnt1.list_id in ('3176', '3295', '3296') 
    and cnt1.listable_type = 'Movie'
    ;

您确实可以选择在join语句中包含第二个选项,我正在用join语句中的逻辑探索第二个选项,但是我看到了一件奇怪的事情。在本例中,lists.ordered=1和cnt1.id