Syntax error while appending string separated by commas in mysql

后端 未结 3 1434
后悔当初
后悔当初 2021-01-25 22:31

I have following query

SELECT diamondsList.*, dealers.* FROM diamondsList JOIN dealers ON diamondsList.dealerId = dealers.id WHERE price >= :minPrice AND pric         


        
相关标签:
3条回答
  • 2021-01-25 23:05

    I have a string "SI2,VS1,SI1,VS2,VVS2,VVS1,IF,FL" How would i do this

    If you have values in CSV format, then you need FIND_IN_SET to search in it.

    Change:

    clarity IN (SI2,VS1,SI1,VS2,VVS2,VVS1,IF,FL)
    

    To:

    find_in_set( clarity, 'SI2,VS1,SI1,VS2,VVS2,VVS1,IF,FL' )
    

    And it seems, the same issue persists in some other parts of your query.
    You need to apply the same FIND_IN_SET on those parts.

    0 讨论(0)
  • 2021-01-25 23:06

    Your string values must be in quotes:

    IN (SI2,VS1,SI1,VS2,VVS2,VVS1,IF,FL)
    

    should be

    IN ('SI2','VS1','SI1','VS2','VVS2','VVS1','IF','FL')
    

    This applies to all of the strings in your query (i.e. check your other IN() statements)

    0 讨论(0)
  • 2021-01-25 23:12

    Hi Umer PLease use the below query

    SELECT diamondsList.*, dealers.* FROM diamondsList 
      JOIN dealers ON diamondsList.dealerId = dealers.id 
    WHERE price >= :minPrice AND price <= :maxPrice 
      AND carat >= :minCarat AND carat <= :maxCarat 
      AND clarity IN ("SI2","VS1","SI1","VS2","VVS2","VVS1","IF","FL") 
      AND color IN ("J","H","D","E","F","G","I") 
      AND diamondsType IN ("BR","HS","CUS","RAD","AS","PRIN","OV","PS","MQ","EM")
    

    If "SI2,VS1,SI1,VS2,VVS2,VVS1,IF,FL" is a single string the you dont need to use IN() you can just use LIKE operator..

    SELECT diamondsList.*, dealers.* FROM diamondsList 
      JOIN dealers ON diamondsList.dealerId = dealers.id 
    WHERE price >= :minPrice AND price <= :maxPrice 
      AND carat >= :minCarat AND carat <= :maxCarat 
      AND clarity LIKE "SI2,VS1,SI1,VS2,VVS2,VVS1,IF,FL" 
      AND color IN ("J","H","D","E","F","G","I") 
      AND diamondsType IN ("BR","HS","CUS","RAD","AS","PRIN","OV","PS","MQ","EM")
    

    What syntax error is there near 'FL? Answer is before FL there IF and IF is a keyword in mySQL. so when you will user quotes the it will remove the error..

    And if you have a string of "SI2,VS1,SI1,VS2,VVS2,VVS1,IF,FL" and you want to find it in individual elements then use following query.

    SELECT diamondsList.*, dealers.* FROM diamondsList 
      JOIN dealers ON diamondsList.dealerId = dealers.id 
    WHERE price >= :minPrice AND price <= :maxPrice 
      AND carat >= :minCarat AND carat <= :maxCarat 
      AND FIND_IN_SET(clarity,"SI2,VS1,SI1,VS2,VVS2,VVS1,IF,FL")  
      AND FIND_IN_SET(color,"J,H,D,E,F,G,I") 
      AND FIND_IN_SET(diamondsType,"BR,HS,CUS,RAD,AS,PRIN,OV,PS,MQ,EM")
    

    Feel free to ask question if you still dont understand..

    0 讨论(0)
提交回复
热议问题