多 sql select 语句
multi sql select statement
我有一个 table 这种:
| name | salary | day | month |
| james | 200.00 | 2 | january |
| marie | 400.00 | 4 | january |
| jimmy | 300.00 | 7 | january |
| Fredd | 700.00 | 3 | february |
| rosieli | 500.00 | 5 | february |
| rosela | 800.00 | 6 | february |
如果 table 名称是 'db_table',我如何将 sql select 查询写入 select 从 1 月 4 日到 2 月 5 日的记录。
类似于:
select * from db_table between day='4',month='january' and day='5' and month='february'";
请问我如何编写正确的 sql 语句来获得所需的 results.so table 如下所示:
| name | salary | day | month |
| marie | 400.00 | 4 | january |
| jimmy | 300.00 | 7 | january |
| Fredd | 700.00 | 3 | february |
| rosieli | 500.00 | 5 | february |
谢谢
您必须从 month
和 day
中创建一个可比较的字符串以在 between
语句中使用:
select * from db_table where
concat(case month
when 'january' then '01'
when 'february' then '02'
........................
when 'december' then '12'
end, case when day < 10 then '0' else '' end, day) between '0104' and '0205'
像这样,您可以通过仅修改开始日期和结束日期来比较任何日期范围。
how do I write an sql select query to select records from 4th January to 5th February.
select *
from db_table
where (month = 'january' and 4 <= cast(day as int)) or
(month = 'february' and cast(day as int) <= 5)
请注意,table 具有单独的月份和日期列的设计使得查询变得困难。在年份界限上会变得更加困难。更好的设计是使用数据库的本机日期时间列类型。然后你可以这样查询:
select *
from db_table
where dt_col between '2019-01-04' and '2019-02-05'
您需要将日期设为数字,但就是这样:
SELECT *
FROM db_table
WHERE (day >= 4 and month = 'January')
OR (day <= 5 and month = 'February')
例如一月到四月:
SELECT *
FROM db_table
WHERE (day >= 4 and month = 'January')
OR (day <= 5 and month = 'April')
OR month IN ('February','March')
你真的应该使用日期来做到这一点。
select t.*
from t
where str_to_date(concat_ws(2020, month, day), '%Y %M %d') between '2020-01-04' and '2020-02-05';
如果可能,应使用日期进行日期比较。
我用的是2020
因为是闰年所以会处理2月29日
解决此问题后,您应该修复数据模型以包含实际日期而不是 month/day 组合。
我有一个 table 这种:
| name | salary | day | month |
| james | 200.00 | 2 | january |
| marie | 400.00 | 4 | january |
| jimmy | 300.00 | 7 | january |
| Fredd | 700.00 | 3 | february |
| rosieli | 500.00 | 5 | february |
| rosela | 800.00 | 6 | february |
如果 table 名称是 'db_table',我如何将 sql select 查询写入 select 从 1 月 4 日到 2 月 5 日的记录。
类似于:
select * from db_table between day='4',month='january' and day='5' and month='february'";
请问我如何编写正确的 sql 语句来获得所需的 results.so table 如下所示:
| name | salary | day | month |
| marie | 400.00 | 4 | january |
| jimmy | 300.00 | 7 | january |
| Fredd | 700.00 | 3 | february |
| rosieli | 500.00 | 5 | february |
谢谢
您必须从 month
和 day
中创建一个可比较的字符串以在 between
语句中使用:
select * from db_table where
concat(case month
when 'january' then '01'
when 'february' then '02'
........................
when 'december' then '12'
end, case when day < 10 then '0' else '' end, day) between '0104' and '0205'
像这样,您可以通过仅修改开始日期和结束日期来比较任何日期范围。
how do I write an sql select query to select records from 4th January to 5th February.
select *
from db_table
where (month = 'january' and 4 <= cast(day as int)) or
(month = 'february' and cast(day as int) <= 5)
请注意,table 具有单独的月份和日期列的设计使得查询变得困难。在年份界限上会变得更加困难。更好的设计是使用数据库的本机日期时间列类型。然后你可以这样查询:
select *
from db_table
where dt_col between '2019-01-04' and '2019-02-05'
您需要将日期设为数字,但就是这样:
SELECT *
FROM db_table
WHERE (day >= 4 and month = 'January')
OR (day <= 5 and month = 'February')
例如一月到四月:
SELECT *
FROM db_table
WHERE (day >= 4 and month = 'January')
OR (day <= 5 and month = 'April')
OR month IN ('February','March')
你真的应该使用日期来做到这一点。
select t.*
from t
where str_to_date(concat_ws(2020, month, day), '%Y %M %d') between '2020-01-04' and '2020-02-05';
如果可能,应使用日期进行日期比较。
我用的是2020
因为是闰年所以会处理2月29日
解决此问题后,您应该修复数据模型以包含实际日期而不是 month/day 组合。