mysql中查询数据库中表名称和结构的sql语句是啥啊啊

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql中查询数据库中表名称和结构的sql语句是啥啊啊相关的知识,希望对你有一定的参考价值。

在网上的服务器中数据库里面建表但是没有可视化的视图都是用sql命令建的,建完后也看不见,只能用sql语句查询可是又不知道sql语句怎么写

TABLE 语句

具体语法:TABLE table_name [ORDER BY column_name] [LIMIT number [OFFSET number]]
其实从语法上看,可以排序,也可以过滤记录集,不过比较简单,没有 SELECT 那么强大。

示例 1

简单的建一张很小的表 y1,记录数为 10 条。表 t1,插入 10 条记录

    mysql-(ytt/3305)->create table t1 (r1 int,r2 int);

    Query OK, 0 rows affected (0.02 sec)

    mysql-(ytt/3305)->insert into t1

    with recursive aa(a,b) as (

    select 1,1

    union all

    select a+1,ceil(rand()*20) from aa where a < 10

    ) select * from aa;

    Query OK, 10 rows affected (0.00 sec)

    Records: 10  Duplicates: 0  Warnings: 0

    简单全表扫描mysql-(ytt/3305)->select * from t1;+------+------+| r1   | r2   |+------+------+|    1 |    1 ||    2 |    9 ||    3 |    9 ||    4 |   17 ||    5 |   17 ||    6 |   16 ||    7 |    6 ||    8 |    1 ||    9 |   10 ||   10 |    3 |+------+------+10 rows in set (0.00 sec)
    TABLE 结果mysql-(ytt/3305)->table t1;+------+------+| r1   | r2   |+------+------+|    1 |    1 ||    2 |    9 ||    3 |    9 ||    4 |   17 ||    5 |   17 ||    6 |   16 ||    7 |    6 ||    8 |    1 ||    9 |   10 ||   10 |    3 |+------+------+10 rows in set (0.00 sec)
    看下 table 的执行计划mysql-(ytt/3305)->explain table t1 order by r1 limit 2\\G*************************** 1. row ***************************           id: 1  select_type: SIMPLE        table: t1   partitions: NULL         type: ALLpossible_keys: NULL          key: NULL      key_len: NULL          ref: NULL         rows: 10     filtered: 100.00        Extra: Using filesort1 row in set, 1 warning (0.00 sec)
    其实可以看到 TABLE 内部被 MySQL 转换为 SELECT 了。mysql-(ytt/3305)->show warnings\\G*************************** 1. row ***************************  Level: Note   Code: 1003Message: /* select#1 */ select `ytt`.`t1`.`r1` AS `r1`,`ytt`.`t1`.`r2` AS `r2` from `ytt`.`t1` order by `ytt`.`t1`.`r1` limit 21 row in set (0.00 sec)
    那其实从上面简单的例子可以看到 TABLE 在内部被转成了普通的 SELECT 来处理。示例 2应用于子查询里的子表。这里要注意,内表的字段数量必须和外表过滤的字段数量一致。克隆表 t1 结构mysql-(ytt/3305)->create table t2 like t1;Query OK, 0 rows affected (0.02 sec)
    克隆表 t1 数据mysql-(ytt/3305)->insert into t2 table t1;Query OK, 10 rows affected (0.00 sec)Records: 10  Duplicates: 0  Warnings: 0
    table t1 被当做内表,表 t1 有两个字段,必须同时满足 t2 检索时过滤的字段也是两个。mysql-(ytt/3305)->select * from t2 where (r1,r2) in (table t1);+------+------+| r1   | r2   |+------+------+|    1 |    1 ||    2 |    9 ||    3 |    9 ||    4 |   17 ||    5 |   17 ||    6 |   16 ||    7 |    6 ||    8 |    1 ||    9 |   10 ||   10 |    3 |+------+------+10 rows in set (0.00 sec)
    注意:这里如果过滤的字段数量和子表数量不一致,则会报错。
参考技术A

查看表名可用“show tables”。

其中红框部分就是表名,如图:

查询表结构用“desc 表名”:

如查询files表的表结构,则语句为“desc files”

如图:

参考技术B function list_table($db)

$result=mysql_list_tables($db);
$list.="<table border='1' width='800px' style='text-align:center;'>";
while($rows=mysql_fetch_row($result))
$list.="<tr><td>".$rows[0]."</td></tr>";

$list.="</table>";
return $list;
这个是用php写的可以查询某个数据库里的所有表的方法
参考技术C 1.show tables
2.desc 表名本回答被提问者采纳

SQL语句批量查询~

数据库中有表[table1]
结构
id[int,标识]
地点名称[varchar(MAX)]
地点里程[varchar(MAX)]
日期[datetime]

记录有:
1, 北路, 600.158, 2008-10-10
2, 北路, 601.528, 2008-10-10
3, 北路, 604.153, 2008-10-10
4, 北路, 602.453, 2008-10-10
5, 南路, 600.958, 2008-10-10
......

假如有1万条数据。

我要查询:2008-10-10至2008-10-17日之间的 “北路” 600.000 到 605.000 之间每隔0.100有多少条记录?

也就是说,地点里程为600.000-600.099之间有多少条记录,600.100-600.199之间有多少条记录。。
至到604.900-604.999,总共要有50多条查询。

特别注意:(地点里程是varchar(MAX)字符类型,是不是还要转换?)

如果优化查询条件?

如果有高人指点200分送上~QQ:3084532
我的地点里程是字符类型。~~如何在SQL语句中转换?

参考技术A 思路:可以使用存储过程,通过循环、拼接SQL语句,将结果循环插入另一张表用于保存记录数。

----以下是代码实现
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CountTable]') AND type in (N'U'))
DROP TABLE [dbo].[CountTable]
GO
CREATE TABLE [dbo].[CountTable](
[CountNum] [int] NULL
) ON [PRIMARY]

declare @low float
declare @high float
declare @addnum float
declare @define nvarchar(max)
declare @strSql nvarchar(max)
declare @execSql nvarchar(max)
set @low = 600.000
set @high = 605.000
set @addnum = 0.100

while(@low < @high)
begin
set @define = 'declare @CountNum int '
set @strSql = ' select @CountNum = count(*) from table1 where 地点名称=''北路'' and 日期>=''2008-10-10'' and 日期<=''2008-10-17''
and cast(地点里程 as float)>=cast('+''''
+cast(@low as nvarchar(max))+''' as float)'+' and cast(地点里程 as float)<cast('''+cast(@high as nvarchar(max))+''' as float)'
set @execSql = ' insert into CountTable values(@CountNum)'
print @define+@strSql+@execSql
exec (@define+@strSql+@execSql)
set @low = @low + @addnum
end

Go
select * from CountTable
参考技术B 不用这样麻烦!
设计思路:地点里程*10且取整 分组 后就是间隔0.100的分组。注意:地点里程字段必须是有效的数字类型。
select count(id) as 数量,里程分段 from
(select id,地点名称,地点里程,日期,
floor(cast(地点里程 as numeric(12,4))*10) as 里程分段
from table1
where 日期 between '2008-10-10' and '2008-10-17')
group by 里程分段
order by 里程分段
参考技术C SELECT count(*) from table1 where table1.地点名称 = '北路' and table1.日期 >= '2008-10-10'and table1.地点里程 between 600.100 and 600.199;

再编个程序 地点里程每次加0.1执行一次查询,就好了

在MySql中测试通过,地点里程是varchar型,在MySql中也可以直接比较,其他数据库不知道的
参考技术D 感觉charlif 的思路很好,
但就是结果没有将间隔的起始与终止列出来,例如起始 600.100 终止600.199,
SQL语句如下:
declare @low float
declare @high float
declare @addnum float
set @low = 600.000
set @high = 605.000
set @addnum = 0.100
while(@low < @high)

select @low as 起始,@high as 终止,记录数 = count(*) from table1 where 地点名称=''北路'' and 日期>=''2008-10-10'' and 日期<=''2008-10-17''
and cast(地点里程 as float)>=@low and cast(地点里程 as float)<@high
set @low = @low + @addnum
end
如果需要将结果出现在一个结果集中的话,就需要将每个间隔的1条结果插入到一个表中,然后最后从这个表中查询,就和charlif的思路一样,
这个问题算是一个比较经典的应用问题,^_^
呵呵,希望能有帮助,^_^
第5个回答  2008-10-30 SELECT
CAST(L.里程段 AS DECIMAL(10,3)) 里程段,
COUNT(1) 计数
FROM (SELECT FLOOR(CAST(里程 AS FLOAT) * 10) / 10 里程段 FROM [Test] WHERE 地点名称 = '北路' AND 日期 BETWEEN '2008-10-10' AND '2008-10-17') L
GROUP BY 里程段

一句搞定 无需编程之类的

重新修改了一下 测试通过本回答被提问者采纳

以上是关于mysql中查询数据库中表名称和结构的sql语句是啥啊啊的主要内容,如果未能解决你的问题,请参考以下文章

用sql语句实现'查询各部门名称和该部门员工数'

在线批量修改mysql中表结构

SQL语句批量查询~

kettle 的mysql数据库中表输入中如何编辑sql语句

C#中查询数据库中表的信息的语句怎么写

sql语句怎么查询指定表的列名