 |
|
[ SQL ] เรื่องการ Count และการเช็คการอัพเดต |
|
 |
|
|
 |
 |
|
Code (SQL)
select count(type) from(
select distinct type from table
) as tmp
ส่วนการ select record ที่มีการเปลี่ยนแปลง
คงต้องสร้าง field เก็บค่า timestamp ซึ่งจะ update ทุกครั้งที่มีการ update record นั้นๆ
ก่อนทำการ update ให้เก็บค่า max( fieldtimestamp) ไว้ก่อน พออัพเดทเสร็จ ให้คิวรี่เฉพาะrecord ที่มี timestamp มากกว่า ค่า max ที่เก็บเอาไว้
|
 |
 |
 |
 |
Date :
2017-12-09 15:09:35 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (SQL)
select count(xtype) from(
select distinct case type when 3 then 2 else type end as xtype from table
) as tmp
|
 |
 |
 |
 |
Date :
2017-12-09 15:52:01 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คห 3 เป็นการ รวม 2 3 เป็นอันเดียวกัน
แต่ถ้า มีหลายเคส ก็ต้องสร้าง case when เพิ่ม
Code (SQL)
select count(xtype) from(
select distinct
case type
when 3 then 2
when 5 then 4
when 6 then 4
else type
end as xtype from table
) as tmp
หรือสร้าง ตาราง มา config ค่า type ว่าอันไหนมันอันเดียวกัน
|
 |
 |
 |
 |
Date :
2017-12-09 18:06:32 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (SQL)
select count(*)
from (
select distinct g.id
from (
select 'A' as Name, 1 as Type union
select 'B', 1 union
select 'C', 1 union
select 'D', 2 union
select 'E', 2 union
select 'F', 2 union
select 'G', 2 union
select 'H', 3 union
select 'J', 3
) as d
left join (
select 1 as id, '1,2' as types union
select 2, '3,4,5'
) as g
on find_in_set(d.type, g.types)
) as tmp
select
|
 |
 |
 |
 |
Date :
2017-12-12 15:27:36 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|