How to check if specified id does not exist in two tables in MySQL?

qwe001 :

I have two relational tables.

table 1 staffs_items

item_id   int(11)
staff_id  int(11)

table 2 entries_items

item_id   int(11)
entry_id  int(11)

I want to check if an item_id does not exist in both tables.

like this:

SELECT EXISTS ( SELECT * FROM staffs_items AS si WHERE si.item_id = 1 ) AS exist;

+-------+
| exist |
+-------+
|     1 |
+-------+

SELECT EXISTS ( SELECT * FROM entries_items AS ei WHERE ei.item_id = 1 ) AS exist;

+-------+
| exist |
+-------+
|     0 |
+-------+

I want to run those queries in a one liner.

If item_id does not exist in either table, I want to return 0 (false).

How can I do that?

Tim Biegeleisen :

You could just place both exists subqueries into a single select:

SELECT
    EXISTS (SELECT 1 FROM staffs_items WHERE item_id = 1) AS exists_staff_item,
    EXISTS (SELECT 1 FROM entries_items WHERE item_id = 1) AS exists_entries_item;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Check if values exist in two MySQL tables

Check if multiple mysql tables exist

How to check, that "ID" which i put from textbox does not exist in my MySql table?

mysql merging two tables if not exist

How to transfer data between tables, only where id does not exist

Between two tables how does one SELECT the table where the id of a specific value exists mysql (duplicate)

CosmosDB Entity with the specified id does not exist

How to check if two files exist

How to check if column values exist in multiple tables?

MySql - How to select MAX ID from two tables with WHERE clause

How to check if data is already exist to check by ID

Check in MySQL table if my ID exist in the results

MySql The user specified as a definer ('mysql.infoschema'@'localhost') does not exist

Generate a custom unique id and check it does not exist before creating a new user with MySQL and sequelize

Azure Cosmos Python "Entity with the specified id does not exist in the system

Azure Logic App CosmosDB Entity with the specified id does not exist in the system

MySQL - SELECT all from TABLE_1 where id does not exist in two fields of TABLE_2

Mysql - Check if id exists in foreign tables

how to join two mysql tables

How to compare two mysql tables?

Sql - data reference does not exist when comparing two tables

How to check if ID already exist - JS

html how to check whether the element with id exist or not?

Compare two mysql tables for id not in second list

mysql exclude results with same id in two tables

MySQL find where values do not exist when comparing two tables

How do you check if data already exist of that id with discord.js mysql

How to check does element exist by tag?

How to check if array is empty or does not exist?