You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过SQL实现每种水果与所有颜色的全量匹配

解决方案:实现颜色与水果的全组合

要得到每种颜色和所有水果的两两组合,你需要使用SQL的笛卡尔积连接(CROSS JOIN),它会返回两个表中所有行的全部组合,完全匹配你想要的结果。

现有数据查询及结果

  • 获取所有颜色的查询:
SELECT Color FROM table1;

执行结果:

Color
------
red
blue
white
  • 获取所有水果的查询:
SELECT Fruit FROM table2;

执行结果:

Fruit
-------
apple
pear
banana
grape

实现全组合的SQL语句

直接用CROSS JOIN连接两个表即可:

SELECT t1.Color, t2.Fruit
FROM table1 t1
CROSS JOIN table2 t2;

结果说明

这条语句会把table1里的每一条颜色记录,和table2里的每一条水果记录逐一配对,最终生成你期望的完整组合结果:

Color  Fruit
---------------
red    apple
red    pear
red    banana
red    grape
blue   apple
blue   pear
blue   banana
blue   grape
white  apple
white  pear
white  banana
white  grape

内容的提问来源于stack exchange,提问作者ReportGuy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 10:52:02