sql - Problems selecting multiple column - Stack Overflow

I have the following SQL query, which works for me on selecting all alerts which belongs to me or which

I have the following SQL query, which works for me on selecting all alerts which belongs to me or which I need to see from people am connected to in FRIENDS TABLE

----TABLE USERS-----
ID  ACCOUNT
1   80001
2   90001
3   70001

----TABLE FRIENDS-------
ID ACCOUNT  TO_ACCOUNT
1    80001    70001

----TABLE ALERTS-------
ID  ACCOUNT  ALERT  ALERTID
1   80001            Hi     XD3000
SELECT snd.ACCOUNT
FROM ALERTS AS m
JOIN USERS AS snd ON snd.ACCOUNT = m.ACCOUNT
WHERE snd.ACCOUNT IN (
  SELECT ACCOUNT
  FROM FRIENDS
  WHERE ACCOUNT = '80001' OR TO_ACCOUNT = '70001'
)
UNION
SELECT snd.ACCOUNT
FROM ALERTS AS m
JOIN USERS AS snd ON snd.ACCOUNT = m.ACCOUNT
WHERE snd.ACCOUNT IN (
  SELECT TO_ACCOUNT
  FROM FRIENDS
  WHERE TO_ACCOUNT = '70001' OR ACCOUNT = '80001'
)");

Now the problem is when I try to add this so that I can stop seeing all alerts which blocked by me, its not working

----TABLE ALERTS_BLOCKED-------
ID  ACCOUNT    ALERTID
1     80001         XD3000
SELECT snd.ACCOUNT
FROM ALERTS AS m
JOIN USERS AS snd ON snd.ACCOUNT = m.ACCOUNT
WHERE snd.ACCOUNT IN (
  SELECT ACCOUNT
  FROM FRIENDS
  WHERE ACCOUNT = '80001' OR TO_ACCOUNT = '70001'
)
AND WHERE m.ALERTID NOT IN (
  SELECT ALERTID
  FROM ALERTS_BLOCKED
  WHERE ACCOUNT = '80001'
)
UNION
SELECT snd.ACCOUNT
FROM ALERTS AS m
JOIN USERS AS snd ON snd.ACCOUNT = m.ACCOUNT
WHERE snd.ACCOUNT IN (
  SELECT TO_ACCOUNT
  FROM FRIENDS
  WHERE TO_ACCOUNT = '70001' OR ACCOUNT = '80001'
)
AND WHERE m.ALERTID NOT IN (
  SELECT ALERTID
  FROM ALERTS_BLOCKED
  WHERE ACCOUNT = '70001'
)");
`

I have the following SQL query, which works for me on selecting all alerts which belongs to me or which I need to see from people am connected to in FRIENDS TABLE

----TABLE USERS-----
ID  ACCOUNT
1   80001
2   90001
3   70001

----TABLE FRIENDS-------
ID ACCOUNT  TO_ACCOUNT
1    80001    70001

----TABLE ALERTS-------
ID  ACCOUNT  ALERT  ALERTID
1   80001            Hi     XD3000
SELECT snd.ACCOUNT
FROM ALERTS AS m
JOIN USERS AS snd ON snd.ACCOUNT = m.ACCOUNT
WHERE snd.ACCOUNT IN (
  SELECT ACCOUNT
  FROM FRIENDS
  WHERE ACCOUNT = '80001' OR TO_ACCOUNT = '70001'
)
UNION
SELECT snd.ACCOUNT
FROM ALERTS AS m
JOIN USERS AS snd ON snd.ACCOUNT = m.ACCOUNT
WHERE snd.ACCOUNT IN (
  SELECT TO_ACCOUNT
  FROM FRIENDS
  WHERE TO_ACCOUNT = '70001' OR ACCOUNT = '80001'
)");

Now the problem is when I try to add this so that I can stop seeing all alerts which blocked by me, its not working

----TABLE ALERTS_BLOCKED-------
ID  ACCOUNT    ALERTID
1     80001         XD3000
SELECT snd.ACCOUNT
FROM ALERTS AS m
JOIN USERS AS snd ON snd.ACCOUNT = m.ACCOUNT
WHERE snd.ACCOUNT IN (
  SELECT ACCOUNT
  FROM FRIENDS
  WHERE ACCOUNT = '80001' OR TO_ACCOUNT = '70001'
)
AND WHERE m.ALERTID NOT IN (
  SELECT ALERTID
  FROM ALERTS_BLOCKED
  WHERE ACCOUNT = '80001'
)
UNION
SELECT snd.ACCOUNT
FROM ALERTS AS m
JOIN USERS AS snd ON snd.ACCOUNT = m.ACCOUNT
WHERE snd.ACCOUNT IN (
  SELECT TO_ACCOUNT
  FROM FRIENDS
  WHERE TO_ACCOUNT = '70001' OR ACCOUNT = '80001'
)
AND WHERE m.ALERTID NOT IN (
  SELECT ALERTID
  FROM ALERTS_BLOCKED
  WHERE ACCOUNT = '70001'
)");
`
Share Improve this question asked Feb 3 at 3:26 majo majomajo majo 311 silver badge3 bronze badges 1
  • Both queries are syntactically incorrect because of the trailing ") and the AND WHERE clause , the correct syntax for multiple parts to a where clause is simply AND .please review the manual – P.Salmon Commented Feb 3 at 3:28
Add a comment  | 

1 Answer 1

Reset to default 1

Both queries are syntactically incorrect because of the trailing ") and the AND WHERE clause , the correct syntax for multiple parts to a where clause ( IN THIS CASE ) is simply AND .please review the manual https://dev.mysql/doc/refman/8.4/en/expressions.html

SELECT snd.ACCOUNT
FROM ALERTS AS m
JOIN USRS AS snd ON snd.ACCOUNT = m.ACCOUNT
WHERE snd.ACCOUNT IN (
  SELECT ACCOUNT
  FROM FRIENDS
  WHERE ACCOUNT = '80001' OR TO_ACCOUNT = '70001'
)
AND  m.ALERTID NOT IN (
  SELECT ALERTID
  FROM ALERTS_BLOCKED
  WHERE ACCOUNT = '80001'
)
UNION
SELECT snd.ACCOUNT
FROM ALERTS AS m
JOIN USRS AS snd ON snd.ACCOUNT = m.ACCOUNT
WHERE snd.ACCOUNT IN (
  SELECT TO_ACCOUNT
  FROM FRIENDS
  WHERE TO_ACCOUNT = '70001' OR ACCOUNT = '80001'
)
AND  m.ALERTID NOT IN (
  SELECT ALERTID
  FROM ALERTS_BLOCKED
  WHERE ACCOUNT = '70001'
);

https://dbfiddle.uk/gxwR6S2b

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745250647a4618659.html

相关推荐

  • sql - Problems selecting multiple column - Stack Overflow

    I have the following SQL query, which works for me on selecting all alerts which belongs to me or which

    11小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信