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

标签 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(启用)时

我已经搜索了几个小时。 做什么?

最佳答案

对我来说看起来像是一个简单的连接:

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/

相关文章:

php - 删除添加的最后一行 Mysqli

mysql - 禁用 MySQL 的 UNION SELECT 查询

mysql - mysql root用户权限丢失

mysql - 比较MySQL同一列的计数以获得百分比

mysql - 子查询返回多于1行的解决方案for update query using select statement

php - insert into 中的 SQL 语法

Php Excel 编码问题

mysql - groovy : Retrieving diff value 中的 BigDecimal 值存在问题

java - Jdbc列索引超出范围

java - 按下按钮时无法保存到数据库