You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求协助编写SQL语句:筛选VALUE为1的全部行及VALUE为0的首行

SQL Solution to Filter Required Rows

Got it, here's how you can achieve exactly what you need. The approach combines two sets of data: all rows where VALUE equals 1, plus the first row where VALUE equals 0 (using ID to define "first" as shown in your expected result).

-- Fetch all rows with VALUE = 1
SELECT ID, NAME, VALUE, DATE
FROM your_table_name
WHERE VALUE = 1

UNION ALL

-- Fetch the first row with VALUE = 0 (ordered by ID to match your example)
SELECT ID, NAME, VALUE, DATE
FROM your_table_name
WHERE VALUE = 0
ORDER BY ID ASC
LIMIT 1;

Quick Notes:

  • Replace your_table_name with the actual name of your table.
  • If "first row" should be determined by a different column (like DATE instead of ID), just adjust the ORDER BY clause in the second query to use that column.
  • Using UNION ALL instead of UNION is more efficient here since the two result sets are mutually exclusive (no overlap between VALUE=1 and VALUE=0 rows), so we don’t need to waste resources removing duplicates.

内容的提问来源于stack exchange,提问作者MacRtor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:38:31