Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计算MyISAM MySQL表中的不同表_Mysql - Fatal编程技术网

计算MyISAM MySQL表中的不同表

计算MyISAM MySQL表中的不同表,mysql,Mysql,我编写了下面的代码作为从不同的MySQL表获取的示例,我不能使用UNION进行计数,只有第一个值变为正确,而另一个值为空 实际上哪个是第一个,因为我听说我的桌子是MyISAM (SELECT COUNT(*) AS userCount from table_users) UNION (SELECT COUNT(*) AS totalposts from table_stories) 那么解决方案是什么呢?如果希望两个结果都在同一行中,可以使用子查询。测试用例: CREATE TABLE ta

我编写了下面的代码作为从不同的MySQL表获取的示例,我不能使用UNION进行计数,只有第一个值变为正确,而另一个值为空

实际上哪个是第一个,因为我听说我的桌子是MyISAM

(SELECT COUNT(*) AS userCount from table_users)
UNION 
(SELECT COUNT(*) AS totalposts from table_stories)

那么解决方案是什么呢?

如果希望两个结果都在同一行中,可以使用子查询。测试用例:

CREATE TABLE table_users (id int) ENGINE=MYISAM;
CREATE TABLE table_stories (id int) ENGINE=MYISAM;

INSERT INTO table_users VALUES (1), (2), (3);
INSERT INTO table_stories VALUES (1), (2), (3), (4), (5), (6);

SELECT (SELECT COUNT(*) from table_users) userCount,
       (SELECT COUNT(*) from table_stories) totalposts;
+-----------+------------+
| userCount | totalposts |
+-----------+------------+
|         3 |          6 |
+-----------+------------+
1 row in set (0.00 sec)
另一方面,如果您希望将结果放在单独的行中,那么您使用的查询也应该可以工作:

(SELECT COUNT(*) AS count_value from table_users)
UNION
(SELECT COUNT(*) from table_stories);
+-------------+
| count_value |
+-------------+
|           3 |
|           6 |
+-------------+
2 rows in set (0.02 sec)

你期望得到什么样的结果?我刚刚尝试了您的查询,它在MyISAM和INNODB表中都能正常工作。为什么要在这里使用别名?我希望从该查询中得到:2行数据,其中一行的userCount为n,totalposts为NULL,第二行的userCount为NULL,totalposts为m(其中m和n是您的计数)。如果您想要一个同时包含两个结果的列,则组成联合的选择必须具有相同的列名。问题,由于不使用where、group、join,因此在
上对
进行简单的查找
显示表状态
是否完全正确?哇,您的第一个查询很好,但是我不知道为什么第二个不是@Mac:第二次查询是否有
NULL
行?