0

I would like to add another column to this query that counts the number of matching [ip_address] values from another query called [qry_ip_blacklist]

SELECT tbl_ip_addresses.ip_address, tbl_ip_addresses.selected
FROM tbl_ip_addresses
WHERE (tbl_ip_addresses.selected=True);

I have tried the following for the new column:

Expr1:

(SELECT COUNT(*) FROM qry_ip_blacklist WHERE ip_address = ip_address)

But this seems to show the total number of records in [qry_ip_blacklist]

How can I achieve this please?

4

2 に答える 2

0

あなたの質問を正しく理解できた場合は、qry_ip_blacklist テーブルの ip_address の数を示す新しい列を使用して最初のクエリを実行してください。

そうで、tbl_ip_addresses にそれ以上の列がない場合は、次のクエリで実行できます。

SELECT tbl_ip_addresses.ip_address, tbl_ip_addresses.selected, COUNT(qry_ip_blacklist.ip_address )
FROM tbl_ip_addresses
LEFT JOIN qry_ip_blacklist 
ON qry_ip_blacklist .ip_address = tbl_ip_addresses.ip_address
WHERE (tbl_ip_addresses.selected=True)
GROUP BY tbl_ip_addresses.ip_address, tbl_ip_addresses.selected

このクエリがまだ機能しない場合は、ブラックリストの IP アドレスを使用してアドレスのクエリを実行し、持っていない人のために別のクエリを実行できます。

SELECT tbl_ip_addresses.ip_address, tbl_ip_addresses.selected, COUNT(qry_ip_blacklist.ip_address)
FROM tbl_ip_addresses
INNER JOIN qry_ip_blacklist ON qry_ip_blacklist .ip_address = tbl_ip_addresses.ip_address
WHERE (tbl_ip_addresses.selected=True)
GROUP BY tbl_ip_addresses.ip_address, tbl_ip_addresses.selected
UNION
SELECT tbl_ip_addresses.ip_address, tbl_ip_addresses.selected, 0
FROM tbl_ip_addresses
WHERE (tbl_ip_addresses.selected=True) AND tbl_ip_addresses.ip_address NOT INT (SELECT ip_addresses FROM qry_ip_blacklist)
于 2013-07-19T11:46:50.507 に答える
0

私の理解が正しければ、ブラックリスト クエリに GROUP BY 句を追加する必要があります。

SELECT 
 tbl_ip_addresses.ip_address
,tbl_ip_addresses.selected
,BlackList.Count

FROM tbl_ip_addresses 

LEFT JOIN (SELECT COUNT(qry_ip_blacklist.ip_address) AS Count, qry_ip_blacklist.ip_address
FROM qry_ip_blacklist
GROUP BY qry_ip_blacklist.ip_address) AS BlackList 
ON tbl_ip_addresses.ip_address = BlackList.ip_address

WHERE tbl_ip_addresses.selected = True;
于 2013-07-19T13:59:01.443 に答える