基于字段限制MySQL结果
我已经将表中的产品称为字段类型值可能是这些值中的任何一个1,2,3,4 现在,我想得到的结果如下基于字段限制MySQL结果,mysql,sql,Mysql,Sql,我已经将表中的产品称为字段类型值可能是这些值中的任何一个1,2,3,4 现在,我想得到的结果如下 1. Group the results based on 'type' 2. And Limit the results for each group to 5. 如何实现这一点,目前我正在使用以下查询 SELECT *, ( ( CASE WHEN product_title LIKE '%xyz%' THEN 2 ELSE 0
1. Group the results based on 'type'
2. And Limit the results for each group to 5.
如何实现这一点,目前我正在使用以下查询
SELECT
*,
(
(
CASE
WHEN product_title LIKE '%xyz%'
THEN 2
ELSE 0
END
) + (
CASE
WHEN product_description LIKE '%xyz%'
THEN 1
ELSE 0
END
)
) AS relevance
FROM
Products
WHERE (
(
product_title LIKE '%xyz%'
OR product_description LIKE '%xyz%'
)
)
AND product_available = 1
AND product_deleted <> 1
ORDER BY relevance DESC
这里有一个简单的例子
SELECT
p.id,
p.type
FROM
Products p
WHERE
5 > (SELECT COUNT(id) from Products where id < p.id and type = p.type)
我已经为它创建了SQLFIDLE,示例数据和所需输出在哪里?
SELECT
p.id,
p.type
FROM
Products p
WHERE
5 > (SELECT COUNT(id) from Products where id < p.id and type = p.type)