0

我有一个 PHP 表单,它使用了几个由查找表填充的选择框。为了减少每页的查询数量,我试图找到一种方法来创建一个笛卡尔积,该积将输出如下结果:

-----------------------------------------------------------------
|        City       |   State    |    SchoolDistrict            |
-----------------------------------------------------------------
| Adamsville        |     AL     |   Adamsville  Public Schools |
| Brownsville       |     AK     |   Baker-Cow USD              |
| Cowtown           |     AR     |   NULL                       |
| NULL              |     AZ     |   NULL                       |
| NULL              |     CA     |   NULL                       |
-----------------------------------------------------------------

然后我可以遍历每个字段,直到找到 null(或 0 长度字符串)。

当我尝试时:

SELECT a.City, b.State, c.SchoolDistrict FROM Cities a, State b, SchoolDistricts c

它返回:

-----------------------------------------------------------------
|        City       |   State    |    SchoolDistrict            |
-----------------------------------------------------------------
| Adamsville        |     AL     |   Adamsville  Public Schools |
| Brownsville       |     AK     |   Baker-Cow USD              |
| Cowtown           |     AR     |   Baker-Cow USD              |
| Cowtown           |     AZ     |   Baker-Cow USD              |
| Cowtown           |     CA     |   Baker-Cow USD              |
-----------------------------------------------------------------

有任何想法吗?

4

1 回答 1

1

我想出的一种解决方案是

SELECT a.City, "City" type from Cities a 
UNION ALL
SELECT b.State, "State" from States b 
UNION ALL
SELECT c.SchoolDistrict, "SchoolDistrict" from SchoolDistricts c 

这将是一个查询,您可以检查第二列的值并执行必要的操作。

于 2013-05-13T16:53:48.197 回答