Mysql 标记新值和重复值的代码
Mysql Code To Mark New And Duplicate Values
我有一个 mysql table(如下所示)有一些值,我需要获取新值和重复值
我试过了SELECT *,COUNT(SerialNumber) FROM config_log GROUP BY SerialNumber
;
但它 return 只有重复计数。如何找到这个???
谢谢
Table :
Id SerialNumber ConfigDate ProductId
---+--------------+-----------+---------
1 11111 2018-12-22 1
2 22222 2018-12-22 2
3 33333 2018-12-22 3
4 11111 2018-12-23 1
产品Table:
ProductId Name
-----------+------
1 a
2 b
3 c
预期结果
Id SerialNumber ConfigDate Type Name
---+--------------+------------+----------+-------
1 11111 2018-12-22 New a
2 22222 2018-12-22 New b
3 33333 2018-12-22 New c
4 11111 2018-12-23 Duplicate a
您可以尝试以下 -
select id,SerialNumber,ConfigDate, case when num=1 then 'New' else 'Duplicate' end as Type,
productid,name
from
(
SELECT
@row_number:=CASE
WHEN @SerialNumber = SerialNumber THEN @row_number + 1
ELSE 1
END AS num,
@SerialNumber:=SerialNumber as PSerialNumber, id, SerialNumber,ConfigDate,productid,name
FROM
t1 left join product on productid=pid ,(SELECT @SerialNumber:=0,@row_number:=0) AS t
order by SerialNumber
)A
以下 mysql 查询将产生预期结果
SELECT cl.*,
IF (cl.id = (SELECT Min(id)
FROM `config_log`
WHERE cl.serialnumber = serialnumber
ORDER BY id ASC), 'NEW', 'DUPLICATE') AS type,
p.name
FROM `config_log` AS `cl`
INNER JOIN products AS p
ON p.id = cl.productid
已在 mysql Distrib 5.7.24 上针对 Linux (x86_64)
进行测试
过滤结果你可以使用这样的
SELECT cl.*,
IF (cl.id = (SELECT Min(id)
FROM `config_log`
WHERE cl.serialnumber = serialnumber
ORDER BY id ASC), 'NEW', 'DUPLICATE') AS type,
p.name
FROM `config_log` AS `cl`
INNER JOIN products AS p
ON p.id = cl.productid
HAVING type = 'DUPLICATE'
我有一个 mysql table(如下所示)有一些值,我需要获取新值和重复值
我试过了SELECT *,COUNT(SerialNumber) FROM config_log GROUP BY SerialNumber
;
但它 return 只有重复计数。如何找到这个???
谢谢
Table :
Id SerialNumber ConfigDate ProductId
---+--------------+-----------+---------
1 11111 2018-12-22 1
2 22222 2018-12-22 2
3 33333 2018-12-22 3
4 11111 2018-12-23 1
产品Table:
ProductId Name
-----------+------
1 a
2 b
3 c
预期结果
Id SerialNumber ConfigDate Type Name
---+--------------+------------+----------+-------
1 11111 2018-12-22 New a
2 22222 2018-12-22 New b
3 33333 2018-12-22 New c
4 11111 2018-12-23 Duplicate a
您可以尝试以下 -
select id,SerialNumber,ConfigDate, case when num=1 then 'New' else 'Duplicate' end as Type,
productid,name
from
(
SELECT
@row_number:=CASE
WHEN @SerialNumber = SerialNumber THEN @row_number + 1
ELSE 1
END AS num,
@SerialNumber:=SerialNumber as PSerialNumber, id, SerialNumber,ConfigDate,productid,name
FROM
t1 left join product on productid=pid ,(SELECT @SerialNumber:=0,@row_number:=0) AS t
order by SerialNumber
)A
以下 mysql 查询将产生预期结果
SELECT cl.*,
IF (cl.id = (SELECT Min(id)
FROM `config_log`
WHERE cl.serialnumber = serialnumber
ORDER BY id ASC), 'NEW', 'DUPLICATE') AS type,
p.name
FROM `config_log` AS `cl`
INNER JOIN products AS p
ON p.id = cl.productid
已在 mysql Distrib 5.7.24 上针对 Linux (x86_64)
进行测试过滤结果你可以使用这样的
SELECT cl.*,
IF (cl.id = (SELECT Min(id)
FROM `config_log`
WHERE cl.serialnumber = serialnumber
ORDER BY id ASC), 'NEW', 'DUPLICATE') AS type,
p.name
FROM `config_log` AS `cl`
INNER JOIN products AS p
ON p.id = cl.productid
HAVING type = 'DUPLICATE'