我可以在MySQL查询中添加WHERE语句吗?

时间:2017-06-01 03:02:28

标签: php mysql mysqli

我有这个查询声明:

*我从查询中删除了一些代码以制作此帖子,因此请忽略NATURAL JOIN %1$sremote_servers

$template = 'SELECT %1$sserver_homes.*,  
        FROM %1$sserver_homes
        NATURAL JOIN %1$sremote_servers
        NATURAL JOIN %1$sconfig_homes
        LEFT JOIN %1$sgame_mods 
            NATURAL JOIN %1$sconfig_mods
            ON %1$sserver_homes.home_id=%1$sgame_mods.home_id
        LEFT JOIN %1$shome_ip_ports 
            NATURAL JOIN %1$sremote_server_ips 
            ON %1$sserver_homes.home_id=%1$shome_ip_ports.home_id
        WHERE `force_mod_id` IN
        (
            SELECT `force_mod_id`
            FROM `%1$shome_ip_ports`
            WHERE `force_mod_id` = %1$sgame_mods.mod_id OR %1$shome_ip_ports.force_mod_id = 0
        )
        OR %1$shome_ip_ports.force_mod_id IS NULL LIMIT '.$gethome_page_forlimit.','.$home_limit.';';

我需要选择 home_cfg_id

所有的游戏服务器

所以我需要在上面的查询中添加WHERE语句

WHERE `home_cfg_id` IN
(
SELECT `home_cfg_id`
FROM `%1$sserver_homes`
WHERE `home_cfg_id` = '.$home_cfg_id.'
)

1 个答案:

答案 0 :(得分:0)

它将在你的最后一个WHERE子句中。记得在OR语句周围放置()。因此,您的WHERE子句应如下所示:

WHERE [table].home_cfg_id = [your condition] 
    AND (force_mod_id IN ([your select statment]) 
        OR %1$shome_ip_ports.force_mod_id IS NULL)

希望这能让你走上正确的道路。

-----更新---- 这样的事情(确保将[]更改为您需要的代码):

$template = 'SELECT %1$sserver_homes.*,  
                   FROM %1$sserver_homes
                    NATURAL JOIN %1$sremote_servers
                    NATURAL JOIN %1$sconfig_homes
                    LEFT JOIN %1$sgame_mods 
                        NATURAL JOIN %1$sconfig_mods
                        ON %1$sserver_homes.home_id=%1$sgame_mods.home_id
                    LEFT JOIN %1$shome_ip_ports 
                        NATURAL JOIN %1$sremote_server_ips 
                        ON %1$sserver_homes.home_id=%1$shome_ip_ports.home_id
                    WHERE `force_mod_id` IN
                    (
                        SELECT `force_mod_id`
                        FROM `%1$shome_ip_ports`
                        WHERE [table].home_cfg_id [your operation, = or like] [your condition] AND (`force_mod_id` = %1$sgame_mods.mod_id OR %1$shome_ip_ports.force_mod_id = 0
                    )
                    OR %1$shome_ip_ports.force_mod_id IS NULL) LIMIT '.$gethome_page_forlimit.','.$home_limit.';