梳理SQL查询

时间:2014-09-03 15:20:07

标签: mysql sql

查询一个:

"SELECT `id` FROM `$amxadmins` WHERE `steamid` = '$logged[username]'"

查询二:

"SELECT `admin_id` FROM `$admins_servers` WHERE `admin_id` = 'ID FROM QUERY ONE' AND `server_id` = '$serverf[id]'"

你能帮我把它们组合起来吗?

2 个答案:

答案 0 :(得分:2)

您可以将第一个sql语句合并到第二个:

SELECT `admin_id`
FROM `$admins_servers`
WHERE `admin_id` = 
    ( SELECT `id`
      FROM `$amxadmins`
      WHERE `steamid` = '$logged[username]'
    )
AND `server_id` = '$serverf[id]'

我猜只有一个admin_id。如果没有,则需要in而不是=

WHERE `admin_id` in 
    ( ...
    )

答案 1 :(得分:0)

这可以通过简单的内部联接来实现:

SELECT `$admins_servers.admin_id` 
 FROM `$amxadmins` 
   INNER JOIN `$admins_servers` 
   ON `$amxadmins.id = `$admins_servers.admin_id` 
 WHERE `server_id` = '$serverf[id]' 
 AND `steamid` = '$logged[username]'