我在json格式的数据库列中有数据。我想写一个查询,其中pSpecialPrice将是" 1"仅从数据库中获取该产品...
我写了一个查询,但它不起作用..
这里是表格中json格式的数据。
{"pName":"BB8GB OTG USB Flash Drive for Smart Phone/Tablet PC Rose Red","pID":"1","pSKU":"81010229","pShortDescp":"BB8GB OTG USB Flash Drive for Smart Phone/Tablet PC Rose Red","pLongDescp":"BB8GB OTG USB Flash Drive for Smart Phone/Tablet PC Rose Red","pURL":"8gb-otg-usb-flash-drive-for-smart-phone-tablet-pc-rose-red","pCountry":"","pStatus":"1","pPrice":"9.99","pspecialPrice":"1","pDiscount":"25","pspDate":"11/11/2015","pDealDisc":"","pdealDate":"","pmetaTitle":"8GB OTG USB Flash Drive for Smart Phone/Tablet PC Rose Red","pmetaDescp":"8GB OTG USB Flash Drive for Smart Phone/Tablet PC Rose Red","pmetakeys":"8GB OTG USB Flash Drive for Smart Phone/Tablet PC Rose Red","pImgLabel":"8GB OTG USB Flash Drive for Smart Phone/Tablet PC Rose Red","pStock":"26","pproStatus":"1","pLimitPerson":"5","product-color":"Blue","submit":"Submit"}
QUERY I WROTE:
SELECT * FROM `Table_Name` WHERE `Column Name` IN ("pspecialPrice":"1")
任何人都可以帮我解决这个问题。
提前致谢..
答案 0 :(得分:1)
首先,你不应该想要它。最好解析JSON并将内容存储在某个关系表中。但是,如果您无法更改架构或仍需要此架构,请使用LIKE
。
SELECT * FROM `Table_Name` WHERE `Column Name` LIKE '%"pspecialPrice":"1"%'
<强> UPD&GT; 强>
我认为最好将JSON数据放到具有此结构的表中,让我们称之为json_values
,例如:
tab_id name value
1 pName BB8GB OTG USB Flash Drive
1 pspecialPrice 1
tab_id
是表中主键的位置,name
是来自JSON数据的关键,value
是来自JSON
的值。您需要在此表中的字段name
和value
上添加索引。你可以找到这样的项目:
SELECT *
FROM `Table_Name` t
WHERE EXISTS (SELECT 1
FROM `json_values` v
WHERE t.id = v.tab_id
AND v.name = 'pspecialPrice'
AND v.value = '1')
此解决方案将更快且可扩展,您可以添加任何字段,并仍然可以使用普通SQL查询搜索项目。
答案 1 :(得分:1)
我想你想使用LIKE
:
SELECT * FROM `Table_Name` WHERE `Column Name` LIKE "%pspecialPrice%"
然而,这是一个坏主意(性能问题,您的数据未规范化)。更好地存储要单独查询的字段。通过这种方式,可以将它们编入索引,从而更快地进行搜索。