简体   繁体   中英

MySQL - Get Items that do not match from another table

I have two tables (table1 and table2) table1 has two columns day and timeSlot. table2 also has day and timeSlot columns. table1 has rows of date and times, table2 has rows of with inserted data including a date and time. My question is

I am trying to write a query string that will get me of list of dates not taken.

I have tried the following:

But it returns very random rows, in fact it will only take away one of the date and times taken.

SELECT table1.day, table1.timeSlot 
FROM woodlandsBookingVipSalesDates 
INNER JOIN table1 ON table1.day != table2.day AND table1.timeSlot != table2.timeSlot

My question is how do I get a list of day and timeSlot not taken.

your question is not very clear

I assume that woodlandsBookingVipSalesDates contains all used dates and time slots and that table_1 contains all the available dates and time slots

so I would simple do

SELECT table1.day, table1.timeSlot 
FROM table_1 WHERE
(day, timeSlot) NOT IN (SELECT day, timeSlot FROM woodlandsBookingVipSalesDates )

Assuming you meant woodlandsBookingVipSalesDates to be table1 and joining table2 ...

Use NOT EXISTS

SELECT t1.day, t1.timeSlot 
FROM table1 t1
WHERE NOT EXISTS (SELECT NULL
                  FROM table2 t2
                  WHERE t1.day != t2.day 
                  AND t1.timeSlot != t2.timeSlot
                 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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