[英]Select the description of the subjects where all occurrences got more than a 9 in mysql
[英]MySQL select with all where and one or more where not
表結構和數據(我知道IP /域字段中的數據可能沒有多大意義,但這僅用於說明目的):
rec_id | account_id | product_id | ip | domain | some_data
----------------------------------------------------------------------------
1 | 1 | 1 | 192.168.1.1 | 127.0.0.1/test | abc
2 | 1 | 1 | 192.168.1.1 | 127.0.0.1/other | xyz
3 | 1 | 1 | 192.168.1.2 | 127.0.0.1/test | ooo
表具有從ip
和domain
字段組合的唯一索引ip_domain
(因此兩個字段中具有相同值的記錄不可存在)。
在每種情況下,我都知道account_id, product_id, ip, domain
字段的值,並且我需要獲得具有SAME account_id, product_id
值和一個(或兩者) ip, domain
其他行ip, domain
值是不同的。
示例:我知道account_id=1
, product_id=1
, ip=192.168.1.1
, domain=127.0.0.1/test
(因此它與rec_id
1匹配),我需要選擇ID為2和3的記錄(因為記錄2有different domain
和記錄3有different ip
)。
所以,我用了查詢:
SELECT * FROM table WHERE
account_id='1' AND product_id='1' AND ip!='192.168.1.1' AND domain!='127.0.0.1/test'
當然,它返回0行。 看着mysql多處在哪里和哪里都沒有寫道:
SELECT * FROM table WHERE
account_id='1' AND product_id='1' AND installation_ip NOT IN ('192.168.1.1') AND installation_domain NOT IN ('127.0.0.1/test')
我的猜測是這個查詢是相同的(只是格式化不同的方式),所以再次0行。 找到了更多的例子,但沒有一個在我的案例中有用。
語法是正確的,但您使用的是錯誤的邏輯操作
SELECT *
FROM table
WHERE account_id='1' AND product_id='1' AND
(ip != '192.168.1.1' OR domain != '127.0.0.1/test')
Select * from table
Where ROWID <> myRowid
And account_id = '1'
And product_id = '1';
myRowid是dbms為每條記錄提供的唯一ID,在這種情況下,您需要使用select語句檢索它,然后在使用此select時將其傳回。 除了您選擇的行之外,這將返回account_id = 1和product_id = 1的所有行。
如果您的輸入未定義/或者您想要列表,那么您可能會看到Group By子句。 另外,您可以查看group_concat
查詢將類似於:
SELECT ACCOUNT_ID, PRODUCT_ID, GROUP_CONCAT(DISTINCT IP||'|'||DOMAIN, ','), COUNT(1)
FROM TABLE
GROUP BY ACCOUNT_ID, PRODUCT_ID
PS:我沒有安裝mysql,因此查詢語法未經驗證
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.