我的问题主要是我不确定在搜索中如何表达我需要的内容。
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(启用)时
我已经搜索了几个小时。 做什么?
最佳答案
对我来说看起来像是一个简单的连接:
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)
很多方法可以解决这个问题..
关于如果另一个表中的列为零,则 MySQL 选择列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14668878/