如果另一个表中的列为零,则MySQL选择列

时间:2013-02-03 02:24:11

标签: mysql

我的问题主要是我不确定如何在搜索中说出我需要的内容。

mysql> SELECT * FROM `accounts`;
+----+--------+---------+----------+
| id | status | oplevel | username |
+----+--------+---------+----------+
| 10 |      1 |       0 | root     |
| 11 |      1 |       1 | Xylex    |
| 12 |      1 |      16 | Anubis   |
| 13 |      0 |      16 | Kami     |
| 14 |      1 |      16 | Zorn     |
+----+--------+---------+----------+


mysql> SELECT * FROM `networks`;
+-----------+-----------+-------------+-----------------+
| networkid | accountid | networkname | serveraddress   |
+-----------+-----------+-------------+-----------------+
|         1 |        10 | Fakenet     | irc.fakenet.org |
|         2 |        10 | Undernet    | irc.under.net   |
|         3 |        12 | Takenet     | irc.takenet.com |
|         4 |        13 | Examplenet  | irc.example.org |
+-----------+-----------+-------------+-----------------+

我有这个帐户表和这个网络表 我需要一个只返回IRC网络地址的查询,只要相应的帐户ID状态列为1(启用)

我一直在寻找几个小时。 做什么?

3 个答案:

答案 0 :(得分:0)

我已将查询更新为帐户ID。

select acc.id, acc.username, nw.serveraddress from accounts acc join networks nw on acc.id = nw.accountid where acc.status == 1 and acc.id=XXX

答案 1 :(得分:0)

select serveraddress from networks, accounts where networks.accountid = accounts.id and accounts.satus=1;

这是你要找的吗?

答案 2 :(得分:0)

看起来像是一个简单的加入我:

select serveraddress from accounts join networks on id=accountid and id=YOUR_ACCOUNT_ID_HERE

或者

select serveraddress from networks where accountid=YOUR_ID and exists(select * from accounts where id=accountid and status)

很多方法可以解决这个问题..