我运行一个SQL命令,我需要获取表b的计数
SELECT A.ENTITY_ID, B.ENTITY_ID
FROM structure A, equipment B
WHERE A.STRUCTURE_NAME = B.STRUCTURE_NAME
AND A.XXXID in ();
表1
结构
A栏 - 18个字符ID(例如 - asdfghjklzxcvbnmqw)
表2
设备
B列 - 27个字符,其前18个字符恰好是A列的18个字符(例如 - asdfghjklzxcvbnmqwpppooo001)
因此,当我提出查询时,我会得到一个像
这样的列表asdfghjklzxcvbnmqw - asdfghjklzxcvbnmqwpppooo001
asdfghjklzxcvbnmqw - asdfghjklzxcvbnmqwpppooo002
asdfghjklzxcvbnmqw - asdfghjklzxcvbnmqwpppxxx001
asdfghjklzxcvbnmqw - asdfghjklzxcvbnmqwpppeee001
asdfghjklzxcvbnmqw - asdfghjklzxcvbnmqwpppeee001
asdfghjklzxcvbnmqw - asdfghjklzxcvbnmqwpppccc002
我想要的只是计数
asdfghjklzxcvbnmqw 6
答案 0 :(得分:0)
您可以使用like
或使用字符串操作进行比较:
SELECT s.ENTITY_ID, e.ENTITY_ID
FROM structure s JOIN
equipment e
ON b.STRUCTURE_NAME = SUBSTR(e.STRUCTURE_NAME, 1, 18)
WHERE s.XXXID in (. . .);
请注意我所做的其他更改:
JOIN
语法。简单规则:从不在FROM
子句中使用逗号。编辑:
如果您想要计数,请使用count(*)
:
SELECT COUNT(*)
FROM structure s JOIN
equipment e
ON b.STRUCTURE_NAME = SUBSTR(e.STRUCTURE_NAME, 1, 18)
WHERE s.XXXID in (. . .);
或者也许:
SELECT s.ENTITY_ID, COUNT(*)
FROM structure s JOIN
equipment e
ON b.STRUCTURE_NAME = SUBSTR(e.STRUCTURE_NAME, 1, 18)
WHERE s.XXXID in (. . .)
GROUP BY s.ENTITY_ID;
答案 1 :(得分:0)
如果您想要的话,可以尝试以下答案
将视图vTemp创建为 SELECT Count(A.ENTITY_ID)为countA,A.ENTITY_ID 从结构A
SELECT A.countA,A.ENTITY_ID,B.ENTITY_ID 来自vTemp A,设备B. 在哪里A.STRUCTURE_NAME = B.STRUCTURE_NAME 和A.XXXID in();
答案 2 :(得分:0)
此SELECT A.ENTITY_ID, count(*)
FROM structure A, equipment B
WHERE A.STRUCTURE_NAME = B.STRUCTURE_NAME
AND A.XXXID in ()
group by a.entity;
对您不起作用?