FIND_IN_SET() vs IN()

前端 未结 6 796
滥情空心
滥情空心 2020-11-22 06:20

I have 2 tables in my database. One is for orders, and one is for companies.

Orders has this structure:

OrderID     |     attachedCompanyIDs
-------         


        
6条回答
  •  心在旅途
    2020-11-22 07:23

    Let me explain when to use FIND_IN_SET and When to use IN.

    Let's take table A which has columns named "aid","aname". Let's take table B which has columns named "bid","bname","aids".

    Now there are dummy values in Table A and Table B as below.

    Table A

    aid aname

    1 Apple

    2 Banana

    3 Mango

    Table B

    bid bname aids

    1 Apple 1,2

    2 Banana 2,1

    3 Mango 3,1,2

    enter code here
    

    Case1: if you want to get those records from table b which has 1 value present in aids columns then you have to use FIND_IN_SET.

    Query: select * from A JOIN B ON FIND_IN_SET(A.aid,b.aids) where A.aid = 1 ;

    Case2: if you want to get those records from table a which has 1 OR 2 OR 3 value present in aid columns then you have to use IN.

    Query: select * from A JOIN B ON A.aid IN (b.aids);

    Now here upto you that what you needs through mysql query.

提交回复
热议问题