Syntax error while appending string separated by commas in mysql

匆匆过客 提交于 2019-12-02 08:23:33

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.

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)

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..

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!