How to replace OR operator with UNION operator?

前端 未结 3 2037
孤街浪徒
孤街浪徒 2021-01-22 12:02

Here is my query:

SELECT 
    h.id,
    h.subject,
    h.body matnF,
    h.amount,
    h.keywords tags,
    h.closed,
    h.author_id author,
    h.AcceptedAnswe         


        
3条回答
  •  别那么骄傲
    2021-01-22 12:37

    If you are going to get any advantage from using a UNION instead of an OR then make that portion of the query as tiny as it can be, in particular keep the number of columns to a minimum. Do the union before any other joins. I suggest this:

    SELECT
          h.id
        , h.subject
        , h.body      AS matnF
        , h.amount
        , h.keywords  AS tags
        , h.closed
        , h.author_id AS author
        , h.AcceptedAnswer
        , h.type
        , h.visibility
        , h.date_time
        , v.value     AS vote_value
        , u.reputation
        , u.user_fname
        , u.user_lname
        , u.avatar
        , h.author_id AS hasUserId
        , (     SELECT COALESCE(SUM(vv.value), 0)
                FROM votes vv
                WHERE h.id = vv.post_id
                      AND vv.table_code = '$this->table_code'
          )  AS total_votes
        , (     SELECT COUNT(1)
                FROM favorites ff
                WHERE h.type = 0 AND h.id = ff.post_id
                      AND ff.table_code = '$this- >table_code'
          ) AS total_favorites
        , CASE
                WHEN h.type = 0 AND f.id IS NOT NULL THEN '2'
                ELSE '3'
          END  AS favorite
        , CASE
                WHEN h.type = 1 THEN '0'
                WHEN h.amount IS NULL OR h.author_id = :user_id2 THEN '1'
                ELSE EXISTS( select 1
                    from money m
                    where m.user_id = :user_id3 and m.post_id = h.id
                   )
          END paid
    FROM (
          SELECT q.id , q.author_id , q.visibility , q.type FROM quanda q WHERE q.id = :id1
          UNION
          SELECT q.id , q.author_id , q.visibility , q.type FROM quanda q WHERE q.related = :id2
          ) h
          LEFT JOIN votes v ON h.id = v.post_id
                      AND v.user_id = :user_id4
                      AND v.table_code = '$this->table_code'
          LEFT JOIN favorites f ON h.type = 0
                      AND h.id = f.post_id
                      AND f.user_id = :user_id5
                      AND f.table_code = '$this->table_code'
          LEFT JOIN users u ON h.author_id = u.id
                      AND h.visibility = 1
    

提交回复
热议问题