具有多列的Mysql查询
我有两个表,如表A和表B。表A和表B中的列名是具有多列的Mysql查询,mysql,sql,select,join,where,Mysql,Sql,Select,Join,Where,我有两个表,如表A和表B。表A和表B中的列名是 A B ------------------------ ----------------------- | ID | sID | qID | fID | | xmlText | sID | qID | ------------------------ ----------------------- | 1 | 1 | 1 | 213 | | xml |
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 `text`
FROM b
WHERE EXISTS (SELECT *
FROM a
WHERE a.sID = b.sID and a.qID = b.qID AND fID = 213)
这将为您提供如下输出:
--------------------------
| Text | sID | qID | fID |
--------------------------
| xml | 1 | 1 | 213 |
| xml | 1 | 2 | 213 |
| xml | 1 | 3 | 213 |
| xml | 2 | 1 | 213 |
| xml | 2 | 2 | 213 |
| xml | 2 | 3 | 213 |
--------------------------
试试这个:
SELECT b.text, a.sid, a.qid, a.fid
FROM a
LEFT OUTER JOIN b ON a.sID = b.sID AND a.qID = b.qID
WHERE a.fID = 213;
SQL查询:
SELECT `qID`,`sID`, `xmlText` AS `text`
FROM `B`
WHERE EXISTS (
SELECT `sID`, `qID`, `fID` , `id`
FROM `a`
WHERE a.sID = B.sID
AND a.qID=B.qID
AND fID=213
)
您可以将两个表连接在一起,并添加以下条件: sID匹配 qID匹配 表A.fID为213 在select中,可以从tableB中提取文本、sid和qid值,并从tableA中提取fid,如下所示:
SELECT b.xmlText, b.sid, b.qid, a.fid
FROM tableA a
JOIN tableB b ON b.sid = a.sid AND b.qid = a.qid AND a.fid = 213;
文本栏在哪里?在哪个表中?将您尝试的内容粘贴到B表的文本列中@Naveen@Gunaseelan我尝试的是首先根据213计算sID并保存在一个数组中,然后根据sID数组的计数计算QID。它给出了我想要的实际结果,但由于数据量大,需要花费大量时间。我想减少时间,或者说在一个查询中而不是两个查询中优化它。@Gunaselan选择s.territoryID、t.name、s.sectionName、s.attributeName、s.shopID、,来自scoreanalysis的s.attributeID作为s内部连接区域作为t在s.territoryID=t.ID上,其中s.territoryID在从区域中选择t.ID作为t,其中t.formatID=213,t.TerritoryLevel=349537,t.lft>2,t.rht<397,s.AchighedScore=0,s.ApplicatableScore=0,我将此结果存储在$row数组中,然后将其传递给下一个查询SELECT QUOTESCOMMENT from comments,其中shopID=.$row[shopID]。questionID=.$row[attributeID]这是我尝试过的,工作很好,但需要很多十分之一。如果不是1:1的关系,这会给你重复的答案
SELECT `qID`,`sID`, `xmlText` AS `text`
FROM `B`
WHERE EXISTS (
SELECT `sID`, `qID`, `fID` , `id`
FROM `a`
WHERE a.sID = B.sID
AND a.qID=B.qID
AND fID=213
)
SELECT b.xmlText, b.sid, b.qid, a.fid
FROM tableA a
JOIN tableB b ON b.sid = a.sid AND b.qid = a.qid AND a.fid = 213;