如标题所说,我想加入 2 个没有键的表进行比较。
mysql join tables without keys
这个例子描述的是初始情况,但是他是求笛卡尔积。 我想要这样的东西
Table 1: t1
red
blue
big
small
Table 2: t2
cat
dog
person
结果应该是这样的:
red cat
blue dog
big person
small NULL <--- can be empty (not shown)
仅使用 sql 就可以实现这样的事情吗?
最佳答案
由于 MySQL 没有 ROW_ID()
函数,您必须使用递增的用户变量来伪造它:
select adjective, noun
from (select @counter1 := @counter1+1 as counter, adjective
from table1, (select @counter1 := 0) init) t1
left join (select @counter2 := @counter2+1 as counter, noun
from table2, (select @counter2 := 0) init) t2
using (counter)
请注意,这假设表 1 中的形容词总是多于表 2 中的名词。如果您需要让任一表变小,则需要使用全外连接
。正如 Andriy M 提到的,MySQL 没有对此的内置支持,但如果您搜索 SO 或谷歌,您应该找到对其进行编码的方法。
关于没有键/索引的 MYSQL 连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18269662/