mysql查表中有多少字段的sql语句

作者&投稿:梁侨 (若有异议请与网页底部的电邮联系)
mysql如何使用sql语句查看一个表中有几个字段呢~

select count(1),* from columns where table_name =要查的表的名字;

MySQL使用SQL查询时,
可以指定单列,如下:
select uid from dusers
也可以指定多列如下:
select uid,uname from dusers
也可以指定全部列
select * from dusers
以上只是简单查询,对于连接查询的示例类同,只是型式不太一样。
select a.uid,a.uname,b.classname from dusers a,dclass b where a.classid=b.classid
如果是查询某个表的全部可以使用*代替。
select a.*,b.classname from dusers a,dclass b where a.classid=b.classid
。。。。查询的内容很多,不一一列举。
希望对你有帮助 。

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)

  • 注意:这里如果过滤的字段数量和子表数量不一致,则会报错。


select
count(*)
from
information_schema.COLUMNS
where
TABLE_SCHEMA='test'
and
table_name='ceshi'
'test'
那个是库名,你替换一下
'ceshi'那个是表名,你也替换你想查找的表名


mysql数据库中,查询a表的a2字段中是否包含1或2或3,语句应该怎样写?知 ...
如果a2 字段中只有 1,2,3 例如 a2 1 2 3 4 语句为:select * from a where a2 in (1,2,3)如果a2字段中的是含有 1 ,2,3 例如 a2 156 254 35 78 语句为 select * from a where a2 like '%1%' or a2 like '%2%' or a2 like '%3%'...

mysql怎么查看数据库中表的大小
from information_schema.TABLES where table_schema = 'testdb'3、查看库中某个表的大小;select concat(round(sum(DATA_LENGTH\/1024\/1024),2),'MB') as data from information_schema.TABLES where table_schema = 'testdb'and table_name = 'test_a';4、查看mysql库中,test开头的表,所有...

mysql中查询一个表里面的某一条件出现最多的所有项
查询人数最多的一个班级的classid用下面的语句:SELECT classid,count(*) FROM students GROUP BY 1 ORDER BY 2 DESC LIMIT 1 组合为你最后结果需要对以上查询进行关联,简单的方法也可以使用视图或者临时表,我相信你是会的。

mySQL中如何查询指定的表中是否存在某个列?
2、查看系统视图tables,在系统视图中可以查到刚建的数据表,select * from information_schema.tables t where table_name = 'test_users',3、查看系统视图columns,在系统视图中可以查到该表所有的字段,select * from information_schema.columns t where table_name = 'test_users',4、查询表中不...

在mysql中有下面一张表,我想查询有大于一条记录的名字数量,请问这句...
SELECT COUNT(*) FROM (SELECT 姓名 FROM 表名 GROUP BY 姓名 HAVING COUNT(*)>1) A

php+mysql 数据库中,如果有一个表中有上万条信息,循环每次读取500信息...
mysql的分页语句里面也有的啊:select * from table limit 0,500;0表示从头开始读,读500条,一般来说都是用变量去控制这两个值,例如:表示当前页数的变量为: $currentPage,表示每页读多少条的变量为:$pageSize;那么这个sql语句成了:sql = "select * from table limit." ($currentPage-1)*$...

怎么用mysql查询全表最大字段值的数据?
(二) FROM子句FROM子句指定SELECT语句查询及与查询相关的表或视图。在FROM子句中最多可指定256个表或视图,它们之间用逗号分隔。在FROM子句同时指定多个表或视图时,如果选择列表中存在同名列,这时应使用对象名限定这些列所属的表或视图。例如在usertable和citytable表中同时存在cityid列,在查询两个表中...

mysql如何查询表 A 中 x 字段共有多少种情况,并输出条数大于 200 的...
select x from 表 group by x having count(x)>200

如何查询mysql数据库中多个表(不固定)今天新增的数据条数,表中都有cre...
不得不说你我有缘,我也是遇到和你这个90%相似的需求,结果发现你这个没答案 我是冥思苦想,研究了1天终于研究出来怎么搞了 你可以参考我的帖子 【MedusaSTears】MySQL存储过程循环查询数据库 我的需求是 查询 某个库 所有表 某年某月的新增数据 先从information_schema.TABLES查询出库名,表名 然后用...

查询mysql数据库中所有表名
查询数据库中所有表名有两种方法:1、select table_name from information_schema.tables where table_schema='当前数据库';2、show tables;其中,information_schema这张数据表保存了MySQL服务器所有数据库的信息。如数据库名,数据库的表,表栏的数据类型与访问权限等。再简单点,这台MySQL服务器上,...

芝罘区18131956752: mysql如何使用sql语句查看一个表中有几个字段呢 -
禤视银翘: select count(1),* from columns where table_name =要查的表的名字;

芝罘区18131956752: mysql查表中有多少字段的sql语句 -
禤视银翘: select count(*) from information_schema.COLUMNS where TABLE_SCHEMA='test' and table_name='ceshi''test' 那个是库名,你替换一下 'ceshi'那个是表名,你也替换你想查找的表名

芝罘区18131956752: mysql中怎么查询表中的字段个数 -
禤视银翘: 方法一,在你的程序中直接desc tablename然后总行数就是你的字段数.mysql> desc ysks;+-------+---------------+-----| Field | Type | Null+-------+---------------+-----| 单号 | int(11) | YES| 金额 | decimal(10,2) | YES| 已收 | decimal(10,2) | YES| 日期 | bigint(20)...

芝罘区18131956752: mysql 怎样查询一个表有多少个字段 -
禤视银翘: select count(*) from information_schema.COLUMNS where TABLE_SCHEMA='test' and table_name='ceshi' 'test' 那个是库名,你替换一下 'ceshi'那个是表名,你也替换你想查找的表名

芝罘区18131956752: 查询出Mysql数据库中一个表的所有字段???? -
禤视银翘: --通过如下语句得到当前Sql server中所有的数据的名称: use master select [name] from [sysdatabases] order by [name] go-- 查询数据库中的所有用户表 use [DBname] select [id], [name] from [sysobjects] where [type] = 'u' order by [name]--通过上...

芝罘区18131956752: mysql数据库查询某个表有哪些字段 -
禤视银翘: SHOW COLUMNS FROM 表 这个命令就可以查询出具体有哪些字段了

芝罘区18131956752: MYSQL 如何查看有多少条数据 -
禤视银翘: 用count函数就可以查看. 比如表名叫test. 要查询表中一共有多少条记录 select count(*) from test;如果按条件查询的话,就正常使用where条件即可select count(*) from test where id=1;

芝罘区18131956752: 查询表中总共有多少个字段的sql语句是什么格式 -
禤视银翘: select count(*) from sys.columns where object_id = object_id('表名')

芝罘区18131956752: 怎样用SQL查询一个表的所有字段 -
禤视银翘: 可以用一句sql语句查询解决,如要查test表中的所有字段及类型1 2 3 4 5Selectb.nameasTableName,C.nameASTYPE fromsyscolumns a,sysobjects b,systypes c wherea.id = b.id andb.type = 'U' anda.xtype=c.xtype andb.name='TEST'; 结果截图:

芝罘区18131956752: 在SQL数据库中,怎么查询一个表中有哪些字段 -
禤视银翘: 在2005之后的版本 select a.* from sys.columns a,sys.tables b where a.object_id = b.object_id and b.name = '要查的表名'

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网