简体   繁体   English

该表或该表中存在mysql id

[英]mysql id exists in this table or that table

I am querying a mysql db. 我正在查询mysql数据库。 I have 3 tables: 我有3张桌子:

Officials 官员们

id | first | last
------------------
1  | jim   | smith
2  | john   | Doe
3  | larry  | toad
4  | Dave   | Charles

Games 游戏类

id | gm_date    | league | REF | UMP
--------------------------------
1  | 2015-03-27 | 1      |   4 | 
2  | 2015-03-28 | 1      |     | 
3  | 2015-03-29 | 1      |     | 
4  | 2015-04-01 | 1      |     | 2

Events 大事记

id | date       | league | off | 
--------------------------------
1  | 2015-03-27 | 1      | 1   | 
2  | 2015-03-28 | 1      |     | 
3  | 2015-03-29 | 1      |     | 
4  | 2015-04-01 | 1      |     | 

I want to query the query both the games table and the events table to see if the id is there for a given date. 我想同时查询游戏表和事件表,以查看ID是否存在给定日期。 If it is in one or both I would like to return true. 如果它在一个或两个中,我想返回true。

Here is my query so far but it doesn't seem to be working. 到目前为止,这是我的查询,但似乎没有用。

SELECT id , first , last
                    FROM officials AS o
                    WHERE o.id = 4 &&
                    EXISTS (

                    SELECT *
                    FROM games AS g
                    WHERE g.REF = o.id && g.gm_date = '2015-03-27' && g.league = 1
                    ) ||
                    EXISTS (

                    SELECT *
                    FROM events as e
                    WHERE e.off = o.id && e.date = '2015-03-27' && e.league = 1
                    )

It is returning all of the ids for the date not the Id that I am requesting. 它返回的是该日期的所有ID,而不是我请求的ID。

Any help would be greatly appreciated. 任何帮助将不胜感激。 I hope I am describing well enough? 我希望我描述得足够好吗?

Maybe something like this: 也许是这样的:

SELECT 
    id , 
    first , 
    last
FROM officials AS o
WHERE o.id = 4 
AND
(
    SELECT 
        NULL 
    FROM 
        games AS g
    WHERE 
        g.REF = o.id 
        AND g.gm_date = '2015-03-27' 
        AND g.league = 1
    UNION ALL
    SELECT
        NULL
    FROM 
        events as e
    WHERE 
        e.off = o.id 
        AND e.date = '2015-03-27' 
        AND e.league = 1
)

Perhaps: Seems odd that you're not also looking at UMP in games table. 也许:奇怪的是,您还没有在游戏表中查看UMP。 I used a join over exists incase you need to return additional data about the game or event. 如果您需要返回有关游戏或事件的其他数据,我曾经使用过存在联接。

SELECT o.id, o.first, o.last
FROM officials o
LEFT JOIN Games g
  on g.Ref = O.id
 and g.gm_date = '2015-03-27'
 and g.league = 1
LEFT JOIN Events e
  on e.Off = O.Id
 and e.league = 1
 and e.date = '2015-03-27'
GROUP BY o.id, o.first, o.last

I'm guessing you want all officials with id 4 where a corresponding row exists in games or in events . 我猜您希望所有ID为4的官员在gamesevents存在相应的行。 If that's the case, then you need parenthesis around your exists conditions because without the parenthesis the query returns all rows where the official id is 4 and a corresponding row exists in games plus all rows where the official (with any id) has a corresponding row in events 如果真是这样,那么您需要在存在的条件周围加上括号,因为如果没有括号,查询将返回官方ID为4且games存在对应行的所有行以及官方(具有任何ID)具有对应行的所有行在events

SELECT id , first , last
FROM officials AS o
WHERE o.id = 4 &&
(
    EXISTS (
        SELECT *
        FROM games AS g
        WHERE g.REF = o.id && g.gm_date = '2015-03-27' && g.league = 1
    ) || EXISTS (
        SELECT *
        FROM events as e
        WHERE e.off = o.id && e.date = '2015-03-27' && e.league = 1
    )
)

I think you're being bitten by the order of precedence between the AND and OR logical operators. 我认为您被ANDOR逻辑运算符之间的优先顺序所困扰。

SELECT  0 AND 1  OR 1
     , (0 AND 1) OR 1
     ,  0 AND (1 OR 1)

The addition of parens around AND/OR conditions allow you explicitly specify the order of precedence you want them evaluated. 通过在AND / OR条件周围添加括号,您可以明确指定要对其进行评估的优先级顺序。

This is the query that seems to be working. 这似乎是有效的查询。

   SELECT 
       id , 
       first , 
       last
   FROM officials AS o
   WHERE o.id = 4 
   AND
   (
       SELECT 
           g.REF 
       FROM 
           games AS g
       WHERE 
           g.REF = o.id 
           AND g.gm_date = '2015-03-27' 
           AND g.league = 1
       UNION ALL
       SELECT
            e.off
       FROM 
           events as e
       WHERE 
           e.off = o.id 
           AND e.date = '2015-03-27' 
           AND e.league = 1
   )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM