How to write the mysql query for return 4 rows exactly
I want to display the 4 rows from the table. I have using following query
SELECT field_name FROM tbl_name WHERE col_name = 'value' LIMIT 4;
The table contains more than 4 values means it display the first 4 rows perfectly. But it less than 4 rows means I need the result for following result
field_name
row1
row2
row2
row1
field_name
row1
row2
row3
row1
How can i get this result?
像这样尝试
Select * from (
select * from tbl_name WHERE col_name = 'value' LIMIT 4
union all
select * from tbl_name WHERE col_name = 'value' LIMIT 4
union all
select * from tbl_name WHERE col_name = 'value' LIMIT 4
union all
select * from tbl_name WHERE col_name = 'value' LIMIT 4) as table1 LIMIT 4
不是一个很好的解决方案,但它应该可以工作:您可以使用UNION ALL重复查询4次,并限制为4个结果:
SELECT * FROM (
SELECT field_name FROM tbl_name WHERE col_name = 'value' LIMIT 4;
UNION ALL
SELECT field_name FROM tbl_name WHERE col_name = 'value' LIMIT 4;
UNION ALL
SELECT field_name FROM tbl_name WHERE col_name = 'value' LIMIT 4;
UNION ALL
SELECT field_name FROM tbl_name WHERE col_name = 'value' LIMIT 4;
) a LIMIT 4
SELECT Column_Name
FROM (
SELECT Column_Name, ROW_NUMBER() OVER (ORDER BY Column_Name) AS RowNum
FROM [dbo].[AttendanceLog]
) AS MyDerivedTable
WHERE MyDerivedTable.RowNum BETWEEN 1 and 4
链接地址: http://www.djcxy.com/p/75334.html
上一篇: MySQL匹配()反对()
下一篇: 如何编写mysql查询以准确返回4行