WHERE语句SQL不起作用

时间:2015-04-17 08:45:55

标签: php mysql sql mysqli

我是SQL和PHP的新手,所以我的知识还不足以解决它。

我希望在我的长查询中有WHERE语句,但它不起作用...

$db->fetch_object_array("SELECT rooms.*, landing_page.url, config_file.config_name,projects.project_name, room_users.hostcode, room_users.guestcode, room_users.first_name, room_users.last_name, 
                   room_users.active, room_users.configuration, config_active, room_users.user_id, rooms.config_id, room_users.trained, license.id AS licenseid
                FROM slots
            LEFT JOIN rooms
              ON rooms.room_id = slots.room_id
            LEFT JOIN room_users
              ON room_users.room_id = rooms.room_id
            LEFT JOIN config_file
              ON config_file.config_id = rooms.config_id
            LEFT JOIN license
              ON license.id = slots.license_id
            LEFT JOIN projects
              ON license.project_id = rooms.project_id
            LEFT JOIN landing_page
              ON landing_page.project_id = license.project_id
            WHERE slots.license_id IN (SELECT id FROM license WHERE id = '{$_GET['license']}' OR parent_id = '{$_GET['license']}')
            GROUP BY rooms.room_id
            ORDER BY slots.license_id ASC, rooms.room_name ASC");

我希望将rooms_config_active配置为GET variable "filtering"但不起作用。

我的变量:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='get'><select id="selectActive" name='filtering'>
                <option value="">Please select a option</option>
                <option value="active">Active</option>
                <option value="inactive">Inactive</option>
            </select>Active / Inactive    <input type='submit' name='filter' value="filter"></th>
        </form>

那么如何使用我的过滤获取变量来完成这项工作呢?我希望之后rooms.config_activeconfig_active

先谢谢你们!

任何类似的东西:

WHERE config_active = $filterting

但它不起作用。

3 个答案:

答案 0 :(得分:0)

如果要在字符串中使用数组,则必须使用".."将它们放入字符串中。只需将$_GET['license']放在那里就行了。

$sql = "SELECT rooms.*, landing_page.url, config_file.config_name,projects.project_name, room_users.hostcode, room_users.guestcode, room_users.first_name, room_users.last_name, 
               room_users.active, room_users.configuration, config_active, room_users.user_id, rooms.config_id, room_users.trained, license.id AS licenseid
            FROM slots
        LEFT JOIN rooms
          ON rooms.room_id = slots.room_id
        LEFT JOIN room_users
          ON room_users.room_id = rooms.room_id
        LEFT JOIN config_file
          ON config_file.config_id = rooms.config_id
        LEFT JOIN license
          ON license.id = slots.license_id
        LEFT JOIN projects
          ON license.project_id = rooms.project_id
        LEFT JOIN landing_page
          ON landing_page.project_id = license.project_id
        WHERE slots.license_id IN (SELECT id FROM license WHERE id = '{".$_GET['license']."}' OR parent_id = '{".$_GET['license']."}')
        GROUP BY rooms.room_id
        ORDER BY slots.license_id ASC, rooms.room_name ASC";
$db->fetch_object_array($sql);

检查错误的最好方法是echo $sql并检查数据库中的语句。

答案 1 :(得分:0)

where子句

WHERE 
slots.license_id IN 
(
SELECT id FROM license WHERE id = '{$_GET['license']}' 
OR parent_id = '{$_GET['license']}'
)

没有意义,您已加入license表,因此您可以将条件用作

where
 ( 
  license.id = '{$_GET['license']}' 
  or license.parent_id = '{$_GET['license']}' 
)

现在添加新过滤器就是

where
 ( 
   license.id = '{$_GET['license']}' 
   or license.parent_id = '{$_GET['license']}' 
 )
 and config_active = '{$_GET['filtering']}'

所以查询变为

SELECT 
rooms.*, 
landing_page.url, 
config_file.config_name,
projects.project_name, 
room_users.hostcode, 
room_users.guestcode, 
room_users.first_name, 
room_users.last_name, 
room_users.active, 
room_users.configuration, 
config_active, 
room_users.user_id, 
rooms.config_id, 
room_users.trained, 
license.id AS licenseid
FROM slots
LEFT JOIN rooms
ON rooms.room_id = slots.room_id
LEFT JOIN room_users
ON room_users.room_id = rooms.room_id
LEFT JOIN config_file
ON config_file.config_id = rooms.config_id
LEFT JOIN license
ON license.id = slots.license_id
LEFT JOIN projects
ON license.project_id = rooms.project_id
LEFT JOIN landing_page
ON landing_page.project_id = license.project_id
where
 ( 
  license.id = '{$_GET['license']}' 
  or license.parent_id = '{$_GET['license']}' 
 )
and config_active = '{$_GET['filtering']}'

GROUP BY rooms.room_id
ORDER BY slots.license_id ASC, rooms.room_name ASC

答案 2 :(得分:0)

几乎修好了..

SELECT rooms.*, landing_page.url, config_file.config_name,projects.project_name, room_users.hostcode, room_users.guestcode, room_users.first_name, room_users.last_name, rooms.config_active,
                   room_users.active, room_users.configuration, room_users.user_id, rooms.config_id, room_users.trained, license.id AS licenseid
                FROM slots
            LEFT JOIN rooms
              ON rooms.room_id = slots.room_id
            LEFT JOIN room_users
              ON room_users.room_id = rooms.room_id
            LEFT JOIN config_file
              ON config_file.config_id = rooms.config_id
            LEFT JOIN license
              ON license.id = slots.license_id
            LEFT JOIN projects
              ON license.project_id = rooms.project_id
            LEFT JOIN landing_page
              ON landing_page.project_id = license.project_id
            WHERE slots.license_id IN (SELECT id FROM license WHERE id = 6 OR parent_id = 6) AND rooms.config_active = 1 // or the variable :)
            GROUP BY rooms.room_id
            ORDER BY slots.license_id ASC, rooms.room_name ASC

感谢您的支持!

使用变量GET它不起作用.. 它只提供无效结果/