1

Mysql id exists in this table or table

 2 years ago
source link: https://www.codesd.com/item/mysql-id-exists-in-this-table-or-table.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Mysql id exists in this table or table

advertisements

I am querying a mysql db. I have 3 tables:

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. If it is in one or both I would like to return 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.

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
)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK