关于如何在php里按条件筛选mysql数据库数据表

此用户名涉嫌违规 2020-10-24 04:33:10
如题,本人想用php按条件选择自己需要的表。再次例子了会按表名里年,月条件进行筛选。但是sql语句该怎么写不知道。想请教一下各位有没有什么办法。举个例,我最新的表是2020年6月份的。我先找近9个月的数据,也就是说2020_6到2019_10这几个表。
...全文
350 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
下雨的声音丶 2020-10-27
  • 打赏
  • 举报
回复
引用 6 楼 此用户名涉嫌违规 的回复:
[quote=引用 5 楼 此用户名涉嫌违规 的回复:][quote=引用 4 楼 下雨的声音丶 的回复:]select table_name from information_schema.tables where table_schema='数据库名称' and table_name like 'road_traffic_counts_hourly_permanent_%' ORDER BY table_name desc; 这就能查到 你这个表,具体你查询出来看一看就知道了
对我当前例子,倒是可以用。但是他排序有点问题。这个语句查出来的结果xx_year_9会在xx_year_12和xx_year_11的前面[/quote] 哦,抱歉,没遇到过这样的排序。你可以查询出来然后用PHP 来排序,也可以 在sql 语句里把这个表名拆分成年 和月 然后用 年 desc ,月 desc 来查询
灵兮爸爸 2020-10-27
  • 打赏
  • 举报
回复
同问
  • 打赏
  • 举报
回复
引用 4 楼 下雨的声音丶 的回复:
select table_name from information_schema.tables where table_schema='数据库名称' and table_name like 'road_traffic_counts_hourly_permanent_%' ORDER BY table_name desc; 这就能查到 你这个表,具体你查询出来看一看就知道了
修改了一下sql语句 select table_name from information_schema.tables where table_schema='990database' and table_name like 'road_traffic_counts_hourly_permanent_%' ORDER BY cast( substring_index(substring_index(table_name,"_",-2),"_",1)as unsigned) DESC, cast(substring_index(table_name,"_",-1)as unsigned) DESC; 成功输出,多谢提供帮助
  • 打赏
  • 举报
回复
引用 5 楼 此用户名涉嫌违规 的回复:
[quote=引用 4 楼 下雨的声音丶 的回复:]select table_name from information_schema.tables where table_schema='数据库名称' and table_name like 'road_traffic_counts_hourly_permanent_%' ORDER BY table_name desc; 这就能查到 你这个表,具体你查询出来看一看就知道了
对我当前例子,倒是可以用。但是他排序有点问题。这个语句查出来的结果xx_year_9会在xx_year_12和xx_year_11的前面
下雨的声音丶 2020-10-24
  • 打赏
  • 举报
回复
select table_name from information_schema.tables where table_schema='数据库名称' and table_name like 'road_traffic_counts_hourly_permanent_%' ORDER BY table_name desc; 这就能查到 你这个表,具体你查询出来看一看就知道了
  • 打赏
  • 举报
回复
引用 2 楼 下雨的声音丶的回复:
mysql 有union 和union all 具体可以参考 https://www.w3school.com.cn/sql/sql_union.asp
其实我最想的还是找到最新的那个表,想图中位数为2020-6的那张
下雨的声音丶 2020-10-24
  • 打赏
  • 举报
回复
mysql 有union 和union all 具体可以参考 https://www.w3school.com.cn/sql/sql_union.asp
  • 打赏
  • 举报
回复
或者,我数据库里面的表最新的是2020年6月份的。也就是sql语句可以按条件选择到XXX_2020_6的那个表就行。

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧