作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
寻找采用下表ProductList
的查询
id| column_1 | column_2 | Sum
================================
1 | Product-A | Product-B | 67
2 | Product-A | Product-C | 55
3 | Product-A | Product-D | 23
4 | Product-B | Product-C | 95
5 | Product-C | Product-D | 110
并返回第一条记录 Product-A_Product-B,然后跳过任一列中包含 Product-A 或 Product-B 的所有记录并返回 Product-C_Product-D。如果该行中的所有内容都是第一次出现,我只想返回该行。
最佳答案
假设产品不包含 ,
,您可以使用逗号分隔的 session 变量来存储已选择的产品,并检查每一行是否其中一列已包含在该变量中:
select column_1, column_2
from (
select l.*,
case when find_in_set(l.column_1, @products) or find_in_set(l.column_2, @products)
then 1
else (@products := concat(@products, ',', l.column_1, ',', l.column_2)) = ''
end as skip
from ProductList l
cross join (select @products := '') init
order by l.id
) t
where skip = 0;
演示:http://rextester.com/NDVBW87988
但你应该知道风险:
ORDER BY
不是真正有效的,通常没有意义。引擎可能会跳过它或将其移动到外部查询。
如果在一条语句中读取和写入同一个 session 变量,则执行顺序没有定义。因此查询可能不适用于所有( future )版本。
关于mysql - 查询 2 列,其中值从未在任一列中多次出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42355107/
背景 我最近在 merge 期间遇到了一个意外未 merge 的文档文件的问题。 无论出于何种原因,我搞砸了 merge 并有效地删除了文件(和其他几个文件),因为我忘记了它们的存在。 现在我想查看我
我在我的网站上使用旧的 mysql 版本和 php 版本 4。 我的表结构: | orders_status_history_id | orders_id | orders_status_id |
我是一名优秀的程序员,十分优秀!