有没有办法根据MySQL中的查询返回唯一值,而无需为每列重新执行相同的查询?
我有一个有点复杂的查询,其中包含一些连接,可能会返回50,000多行。我的目标是获得每列的所有唯一值,就像我已经完成的那样:
SELECT DISTINCT column_a FROM ...
每列。如果不多次运行查询,有没有办法完成这项工作?
答案 0 :(得分:0)
您可以使用列子查询。例子
mysql> select (select name from city order by rand() limit 1) "randdom value1", (select name from city order by rand() limit 1) "random value2", name "actual value" from city limit 10;
+--------------------+-------------------+----------------+
| randdom value1 | random value2 | actual value |
+--------------------+-------------------+----------------+
| Valle de Santiago | Shulin | Kabul |
| Ibarra | Xiangfan | Qandahar |
| Kingston upon Hull | Bataisk | Herat |
| Huancayo | Fort Worth | Mazar-e-Sharif |
| Longyan | Linköping | Amsterdam |
| Cheju | Rubtsovsk | Rotterdam |
| Aligarh | Bergisch Gladbach | Haag |
| Najran | Pultava [Poltava] | Utrecht |
| Shuangyashan | Cedar Rapids | Eindhoven |
| Buffalo | Abilene | Tilburg |
+--------------------+-------------------+----------------+
10 rows in set (0.03 sec)