时间:2021-07-01 10:21:17 帮助过:22人阅读
SELECT * FROM `subject` WHERE Subject_id IN (1602509) OR 1=1 LIMIT 5
比如数据库里面有一条记录 Subject_id =1602509,我的结果集想查询出5条记录,并且包含
Subject_id =1602509这一条记录,SQL该如何写呢??
如果没有Subject_id =1602509这条记录,那么就用其他数据补齐5条记录
SELECT * FROM `subject` WHERE Subject_id IN (1602509) OR 1=1 LIMIT 5
比如数据库里面有一条记录 Subject_id =1602509,我的结果集想查询出5条记录,并且包含
Subject_id =1602509这一条记录,SQL该如何写呢??
如果没有Subject_id =1602509这条记录,那么就用其他数据补齐5条记录
SELECT * FROM `subject` WHERE Subject_id IN (1602509)
UNION
SELECT * FROM `subject` WHERE Subject_id NOT IN (1602509) LIMIT 5
其实这样也行
最好是在业务层实现