MySQL selecting multiple table

huangapple go评论71阅读模式
英文:

MySQL selecting multiple table

问题

我有一个MYSQL数据库,我想选择连接在表users和表friends中的账户。

这是我的查询语句:

SELECT id(distinct FRIENDS.ID) FROM FRIENDS JOIN USERS ON FRIENDS.ACC IN (FRIENDS.ACC, FRIENDS.CONTO)
WHERE #USER FT11 IN (FRIENDS.ACC, FRIENDS.CONTO)

这是我得到的错误:

您的SQL语法有错误;请检查与您的MySQL服务器版本相对应的手册以获取正确的语法使用方法。

表users:

ID USER_ACC NAME
1 FT11 JANE
2 FT22 FRED
3 FT33 KEN

表friends:

ID ACC CONTO
1 FT11 FT33

ACC可以是我的账户,或者CONTO可以是我的账户,所以我想选择或检查我在friends表中与这些账户连接的账户。

英文:

Am having a MYSQL db and i want to select accounts thats connected in table users and table friends

this is my query

SELECT id(distinct FRIENDS.ID) FROM FRIENDS JOIN USERS ON FRIENDS.ACC IN (FRIENDS.ACC, FRIENDS.CONTO) 
    WHERE #USER FT11 IN (FRIENDS.ACC, FRIENDS.CONTO)

This is the error i get

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use

TABLE USERS

ID USER_ACC NAME
1  FT11     JANE
2  FT22     FRED
3  FT33     KEN


TABLE FRIENDS

ID ACC  CONTO
1  FT11 FT33

ACC can be my account or CONTO can be my account
so i want to select or check these accounts am connected with on same column in FRIENDS table

答案1

得分: 1

可能这就是你想要的。

选择 u1.user_acct, u1.name, u2.user_acct, u2.name
从 Friends f
加入 Users u1 ON u1.user_acc = f.acc
加入 Users u2 ON u2.user_acc = f.conto
英文:

Probably this is what you want.

SELECT u1.user_acct, u1.name, u2.user_acct, u2.name
FROM Friends f
JOIN Users u1 ON u1.user_acc = f.acc
JOIN Users u2 ON u2.user_acc = f.conto

答案2

得分: 0

我已经添加了一个代码片段:
SELECT DISTINCT F.ID FROM FRIENDS F JOIN USERS U ON F.ACC = U.USER_ACC OR F.CONTO = U.USER_ACC
WHERE U.USER_ACC = 'FT11';

尝试使用这个。

英文:

I have added a code snippet:
SELECT DISTINCT F.ID FROM FRIENDS F JOIN USERS U ON F.ACC = U.USER_ACC OR F.CONTO = U.USER_ACC
WHERE U.USER_ACC = 'FT11';

Try using this.

huangapple
  • 本文由 发表于 2023年8月9日 03:14:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862598.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定