如何从连接表中订购结果

时间:2016-02-20 16:49:46

标签: mysql sql join sql-order-by

我有以下查询,我尝试按c_priority(c.priority_nl的别名)排序,但这被忽略了。我已经尝试但无法提供解决方案。提供解决方案的提示非常受欢迎!

SELECT e.id                                            AS events_id,
       e.clients_id                                    AS clients_id,
       e.leagues_id,
       e.title                                         AS e_title,
       IF(m1.date_start, m1.date_start, m2.date_start) AS date_time_start,
       l.id                                            AS leagues_id,
       l.title                                         AS leagues_title,
       c.priority_nl                                   AS c_priority,
       p1.id                                           AS p1_id,
       p1.title                                        AS ml_away_title,
       IF(p1.odds_decimal > 0, p1.odds_decimal, 'N/a') AS ml_away_odds,
       p2.id                                           AS p2_id,
       p2.title                                        AS ml_home_title,
       IF(p2.odds_decimal > 0, p2.odds_decimal, 'N/a') AS ml_home_odds,
       p3.id                                           AS p3_id,
       p3.title                                        AS sp_away_title,
       p3.odds_decimal                                 AS sp_away_odds,
       p3.handicap                                     AS sp_away_handicap,
       p4.id                                           AS p4_id,
       p4.title                                        AS sp_home_title,
       p4.odds_decimal                                 AS sp_home_odds,
       p4.handicap                                     AS sp_home_handicap
FROM   fcaopnyp_asp_events e
       LEFT JOIN fcaopnyp_asp_markets m1
              ON m1.events_id = e.id
                 AND ( m1.market_id = 960
                        OR m1.market_id = 1
                        OR m1.title = 'Money Line'
                        OR m1.title = 'Money Line (US)'
                        OR m1.title = 'st'
                        OR m1.title = 'Head to Head' )
       LEFT JOIN fcaopnyp_asp_participants p1
              ON p1.markets_id = m1.id
                 AND p1.title LIKE 'Away%'
       LEFT JOIN fcaopnyp_asp_participants p2
              ON p2.markets_id = m1.id
                 AND p2.title LIKE 'Home%'
       LEFT JOIN fcaopnyp_asp_markets m2
              ON m2.events_id = e.id
                 AND ( m2.market_id = 1453
                        OR m2.market_id = 2
                        OR m2.title = 'Handicap'
                        OR m2.title = 'Spread'
                        OR m2.title = 'Point Spread'
                        OR m2.title = 'ah'
                        OR m2.title LIKE 'Handicap Match Result%' )
       LEFT JOIN fcaopnyp_asp_participants p3
              ON p3.markets_id = m2.id
                 AND p3.title LIKE 'Away%'
       LEFT JOIN fcaopnyp_asp_participants p4
              ON p4.markets_id = m2.id
                 AND p4.title LIKE 'Home%'
       INNER JOIN fcaopnyp_asp_leagues l
               ON e.leagues_id = l.id
       INNER JOIN fcaopnyp_asp_clients c
               ON e.clients_id = c.id
WHERE  e.leagues_id IN( 2 )
       AND ( Date_add(m1.date_start, INTERVAL 2 hour) > Now()
              OR Date_add(m2.date_start, INTERVAL 2 hour) > Now() )
       AND ( ( e.leagues_id != 8
               AND ( m1.date_start < Date_add(Now(), INTERVAL 72 hour)
                      OR m2.date_start < Date_add(Now(), INTERVAL 72 hour) ) )
              OR e.leagues_id = 8 )
ORDER  BY Date(date_time_start),
          Hour(date_time_start),
          Time(date_time_start),
          e_title,
          c_priority
LIMIT  50;

1 个答案:

答案 0 :(得分:0)

您不应该通过c.priority_nl而不是c.priority

订购