简体   繁体   English

如何从MySQL表正确获取结果

[英]How to get result properly from MySQL table

This is my product table 这是我的产品表

在此处输入图片说明

and I want to get all product where product_sizes in (Xl, M) 而且我想获得product_sizes为(Xl,M)的所有产品

I am trying into this code 我正在尝试此代码

SELECT `id`,`name`,`sell_price`,`product_sizes`,`product_colors`
FROM `view_product_listing` 
WHERE `product_sizes` in ('XL', 'M')

return to me productId 15 and 16 but I want to productId 4,14,15,16 返回给我productId 15和16,但我要productId 4,14,15,16

You should seriously avoid storing the sizes as CSV (comma separated) data, see below for an alternative table design. 您应该认真避免将大小存储为CSV(逗号分隔)数据,请参见下文,了解其他表格设计。 As a workaround, we can use FIND_IN_SET : 解决方法是,我们可以使用FIND_IN_SET

SELECT id, name, sell_price, product_sizes, product_colors
FROM view_product_listing
WHERE FIND_IN_SET('M', product_sizes) > 0 OR FIND_IN_SET('XL', product_sizes) > 0;

But note that a much better database design would be to have a separate table for products, sizes, and colors: (colors omitted) 但是请注意,更好的数据库设计是针对产品,尺寸和颜色使用单独的表格:(省略颜色)

products
id | name                                 | sell_price |
4  | Women Maxi White Dress               | 550.00     |
14 | Women Maxi Blue Dress                | 700.00     |
15 | Women Fit and Flare Multicolor Dress | 750.00     |
16 | Floral Print Straight Kurta          | 699.00     |

sizes
product_id | product_size
4          | XL
4          | M
14         | XL
14         | XXL
14         | L
14         | M
15         | XL
16         | M

Now we can use a straightforward join to find all products, and their metadata, which have either the medium or XL size: 现在,我们可以使用直接联接来查找具有中等或XL大小的所有产品及其元数据:

SELECT
    p.id,
    p.name,
    p.sell_price,
    s.product_size
FROM products p
INNER JOIN sizes s
    ON p.id = s.product_id
WHERE
    s.product_size IN ('M', 'XL');

You can use FIND_IN_SET like this, 您可以像这样使用FIND_IN_SET

Version 1: 版本1:

SELECT `id`,`name`,`sell_price`,`product_sizes`,`product_colors`
FROM `view_product_listing` 
WHERE FIND_IN_SET('XL',product_sizes) OR FIND_IN_SET('M',product_sizes)

EDIT 1 编辑1

There is one more approach to achieve this, 还有另一种方法可以做到这一点,

Version 2: 版本2:

SELECT `id`,`name`,`sell_price`,`product_sizes`,`product_colors`
FROM `view_product_listing` 
WHERE CONCAT(',',product_sizes,',') REGEXP ",(XL|M),"

Source link for second version . 第二版的源链接。

EDIT 2 编辑2

You product_sizes is having spaces after commas, which is not the behaviour find_in_set is expecting. product_sizes在逗号后有空格,这不是find_in_set所期望的行为。 To trim all spaces from that column, 要修剪该列中的所有空格,

UPDATE `table` SET `product_sizes` = REPLACE(`product_sizes`, ' ', '')

And now run any version query you want to try, it will work. 现在,运行任何您想尝试的版本查询,它将起作用。

use find_in_set() 使用find_in_set()

SELECT `id`,`name`,`sell_price`,`product_sizes`,`product_colors`
FROM `view_product_listing` 
WHERE FIND_IN_SET(product_sizes,'XL,M') 

If you are only searching for one size you could use the built-in MySQL function FIND_IN_SET() 如果仅搜索一种尺寸,则可以使用内置的MySQL函数FIND_IN_SET()

SELECT `id`,`name`,`sell_price`,`product_sizes`,`product_colors`
FROM `view_product_listing` 
WHERE FIND_IN_SET('XL', product_sizes)

However it only supports a single string (unless you add additional OR 's, but that quickly becomes tedious). 但是,它仅支持单个字符串(除非您添加其他OR ,但这很快就会变得乏味)。

The best approach would be to restructure your database so that the product sizes of items are in a seperate table with a reference to the item table and a size table. 最好的方法是重组数据库,以使商品的产品尺寸位于单独的表中,并带有对商品表和尺寸表的引用。

That way you don't have to update your queries whenever you want to introduce new sizes and it will improve performance for your query. 这样,您就不必在每次要引入新大小时都更新查询,这将提高查询的性能。

A word of warning, 一句话,

Do not attempt a LIKE clause, because search for LIKE '%XL%' will also match XXL 不要尝试使用LIKE子句,因为搜索LIKE '%XL%'也会匹配XXL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM