【发布时间】:2015-02-21 07:06:42
【问题描述】:
我有两个表,例如表 A 和表 B。表 A 和 B 中的列名分别是
A B
------------------------ -----------------------
| ID | sID | qID | fID | | xmlText | sID | qID |
------------------------ -----------------------
| 1 | 1 | 1 | 213 | | xml | 1 | 1 |
| 2 | 1 | 2 | 213 | | xml | 1 | 2 |
| 3 | 1 | 3 | 213 | | xml | 1 | 3 |
| 4 | 2 | 1 | 213 | | xml | 2 | 1 |
| 5 | 2 | 2 | 213 | | xml | 2 | 2 |
| 6 | 2 | 3 | 213 | | xml | 2 | 3 |
| 7 | 4 | 1 | 214 | | xml | 4 | 1 |
------------------------ -----------------------
现在我想编写一个查询,该查询将针对 213 值从表 A 中选择所有 sID 和 qID,并将这些 sID 和 fID 传递给表 B,并一一获取所有文本,如下面的输出所示。
--------------------------
| Text | sID | qID | fID |
--------------------------
| abc | 1 | 1 | 213 |
| abc | 1 | 2 | 213 |
| abc | 1 | 3 | 213 |
| abc | 2 | 1 | 213 |
| abc | 2 | 2 | 213 |
| abc | 2 | 3 | 213 |
--------------------------
我尝试了下面的代码。
SELECT s.territoryID, t.name, s.sectionName, s.attributeName, s.shopID, s.attributeID
FROM scoreanalysis AS s
INNER JOIN territories AS t ON s.territoryID = t.ID
WHERE s.territoryID IN
(
SELECT t.ID FROM territories as t
WHERE t.formatID = 213 and t.territorylevelID =349537
and t.lft > 2 and t.rht < 397
)
AND s.achievedScore =0 AND s.applicableScore !=0
与
SELECT questionComment from comments where
shopID=".$row["shopID"]." and questionID=".$row["attributeID"]
有什么办法可以解决这个问题!有什么帮助吗?提前致谢
【问题讨论】:
-
文本栏在哪里?在哪个表中?
-
粘贴你尝试过的内容
-
text 列在 B 表中。 @Naveen
-
@Gunaseelan 我首先尝试根据 213 计算 sID 并保存在数组中,然后根据 sID 数组的计数计算 qID。它给了我想要的实际结果,但需要很多时间(由于数据量大)我想减少时间,或者说在单个查询中优化它而不是两个。
-
@Gunaseelan SELECT s.territoryID, t.name, s.sectionName, s.attributeName, s.shopID, s.attributeID FROM scoreanalysis AS s INNER JOIN regions AS t ON s.territoryID = t. ID WHERE s.territoryID IN(从区域中选择 t.ID 为 t WHERE t.formatID = 213 and t.territorylevelID =349537 and t.lft > 2 and t.rht
标签: mysql sql select join where