当我通过执行以下操作选择所有内容时,我可以选择查询返回的行数:
SELECT Count(*) FROM `foo`.`bar`;
但是,我现在想要选择返回的列数。有一个简单的方法吗?
答案 0 :(得分:3)
使用information_schema
数据库。
SELECT
COUNT(COLUMN_NAME) AS numcols
FROM COLUMNS
WHERE
TABLE_NAME = 'bar'
AND TABLE_SCHEMA = 'foo'
要将两者组合在一起,请使用:
SELECT
(SELECT COUNT(*) FROM foo.bar) AS numrows,
(SELECT
COUNT(COLUMN_NAME) AS numcols
FROM COLUMNS
WHERE
TABLE_NAME = 'bar'
AND TABLE_SCHEMA = 'foo'
) AS numcols
答案 1 :(得分:2)
DESCRIBE someTable
然后mysql_count_rows()
。
由于问题与C#有关,我认为这应该有用 - 但我很久没有做过C#。
MySqlConnection connection = new MySqlConnection(#...);
MySqlCommand command = connection.CreateCommand();
MySqlDataReader Reader;
command.CommandText = "DESCRIBE sometable";
connection.Open();
Reader = command.ExecuteReader();
Reader.FieldCount # this has the count of rows, and hence columns
答案 2 :(得分:1)
如果您的表格mydb.mytable
是MyISAM,则以下内容应运行良好:
SELECT
row_count,column_count
FROM
(
SELECT table_rows row_count
FROM information_schema.tables
WHERE table_schema = 'mydb'
AND table_name = 'mytable'
) rowcount,
(
SELECT MAX(ordinal_position) column_count
FROM information_schema.columns
WHERE table_schema = 'mydb'
AND table_name = 'mytable'
) columncount
;