我如何在MySQL表上进行SELECT,我将为所有行创建一个值为0的列,除了最后一行将标记为1.是否可能?
示例:
真实表:
+----+----------------------+
| id | schedule_name |
+----+----------------------+
| 1 | Global Fee Schedule |
| 2 | CAT Fee Schedule |
| 3 | Daily Fee Schedule |
| 4 | Daily Claim Schedule |
| 5 | Wind/Hail Schedule |
| 6 | Daily Claim Schedule |
| 7 | Daily Claims |
| 8 | CAT Fee Schedule |
| 9 | Daily Claims |
| 10 | Daily Claims |
+----+----------------------+
添加了专栏:
+----+----------------------+-------------+
| id | schedule_name | last_column |
+----+----------------------+-------------+
| 1 | Global Fee Schedule | 0 |
| 2 | CAT Fee Schedule | 0 |
| 3 | Daily Fee Schedule | 0 |
| 4 | Daily Claim Schedule | 0 |
| 5 | Wind/Hail Schedule | 0 |
| 6 | Daily Claim Schedule | 0 |
| 7 | Daily Claims | 0 |
| 8 | CAT Fee Scedule | 0 |
| 9 | Daily Claims | 0 |
| 10 | Daily Claims | 1 |
+----+----------------------+-------------+
答案 0 :(得分:1)
您可以使用最大功能。例如:
select *, id=(select max(id) from YOUR_TAB) as last_column from YOUR_TAB
答案 1 :(得分:0)
不确定为什么你需要这样的东西。 但是,这是可能的,但不是很容易做到。
创建/插入数据
CREATE TABLE iascat_fee_categories
(`id` INT, `schedule_name` VARCHAR(20))
;
INSERT INTO iascat_fee_categories
(`id`, `schedule_name`)
VALUES
(1, 'Global Fee Schedule'),
(2, 'CAT Fee Schedule'),
(3, 'Daily Fee Schedule'),
(4, 'Daily Claim Schedule'),
(5, 'Wind/Hail Schedule'),
(6, 'Daily Claim Schedule'),
(7, 'Daily Claims'),
(8, 'CAT Fee Schedule'),
(9, 'Daily Claims'),
(10, 'Daily Claims')
;
将last_column标记为1的select查询非常复杂。
选择LIMIT 0,10
SELECT
iascat_fee_categories.id
, iascat_fee_categories.schedule_name
, IF((max_id_counts.max_sum_id_count + 0)= iascat_fee_categories.id, 1, 0) last_column
FROM (
SELECT
SUM(id_counts.id_count) max_sum_id_count
FROM (
SELECT
COUNT(*) id_count
FROM
iascat_fee_categories
GROUP BY
iascat_fee_categories.id
LIMIT 0, 10
) AS id_counts
) AS max_id_counts
CROSS JOIN
iascat_fee_categories
LIMIT 0, 10
<强>输出强>
id schedule_name last_column
------ -------------------- -------------
1 Global Fee Schedule 0
2 CAT Fee Schedule 0
3 Daily Fee Schedule 0
4 Daily Claim Schedule 0
5 Wind/Hail Schedule 0
6 Daily Claim Schedule 0
7 Daily Claims 0
8 CAT Fee Schedule 0
9 Daily Claims 0
10 Daily Claims 1
但是如果你想SELECT LIMIT 1,10选择查询会改变。
请注意,您还需要将此部分IF((max_id_counts.max_sum_id_count + 0)
更新为IF((max_id_counts.max_sum_id_count + 1)
并且包括两个LIMIT语句
并选择LIMIT 2,您需要对IF((max_id_counts.max_sum_id_count + 2)
进行更改
并且包括两个LIMIT语句
SELECT LIMIT 1,10
SELECT
Table1.id
, Table1.schedule_name
, IF((max_id_counts.max_sum_id_count + 1)= iascat_fee_categories.id, 1, 0) last_column
FROM (
SELECT
SUM(id_counts.id_count) max_sum_id_count
FROM (
SELECT
COUNT(*) id_count
FROM
iascat_fee_categories
GROUP BY
Table1.id
LIMIT 1, 10
) AS id_counts
) AS max_id_counts
CROSS JOIN
iascat_fee_categories
LIMIT 1, 10
<强>输出强>
id schedule_name last_column
------ -------------------- -------------
2 CAT Fee Schedule 0
3 Daily Fee Schedule 0
4 Daily Claim Schedule 0
5 Wind/Hail Schedule 0
6 Daily Claim Schedule 0
7 Daily Claims 0
8 CAT Fee Schedule 0
9 Daily Claims 0
10 Daily Claims 1