sql - Use Left outer join to remove duplicate row in Mysql -


i have 2 database table in mysql this.

1).tablestr .

     str_id       table_no             133         t1                  156         t2                  130         t3                  143         t5                  277         t6                  28          t7          

2). booking table

   book_id       booked_by    str_id         1          w          133         2          p          277         3          p          28,130         4          p          156         5          w          277         6          w          143,156         7          p          156,143 

str_id primary key in tablestr table. str_id foregin key in booking table stores str_id comma separted values tablestr table.

i want join 2 table , produce mysql output using condition.

condition involve in query :

1). $cont1=select str_id bookingtable booked_by="w". 2). select * tablestr str_id in ($cont1). 

i want select str_id booking table booked_by="w" selected values need removed tablestr.

so output should this

     str_id       table_no             130         t3                  28          t7          

i can using subquery. it's takes 6 7 seconds there million data. can use left join or other query perform action faster.

select t.str_id, t.table_no tablestr t left join booking b on find_in_set(t.str_id, b.str_id) b.booked_by = 'w'; 

fiddle

edit: seeing ids integers can use locate (probably faster though don't know sure)

select * booking; select t.str_id, t.table_no tablestr t left join booking b on locate(t.str_id, b.str_id) b.booked_by = 'w'; 

fiddle

find_in_set() used find string in list of strings
locate() used find integer value in list of integers

edit try running subquery this, not sure if faster have look:

select str_id, table_no tablestr str_id not in (select t.str_id tablestr t left join booking b on locate(t.str_id, b.str_id) b.booked_by = 'w'); 

edit: try using exists instead of in, might make query faster:

select tstr.str_id, tstr.table_no tablestr tstr not exists (select 1 tablestr t left join booking b on locate(t.str_id, b.str_id) b.booked_by = 'w' , tstr.str_id = t.str_id); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -