2表查询仅显示某些行

时间:2014-01-01 16:05:11

标签: php mysqli

表结构:

  • 客户:

customer_id, first_name, last_name, street, unit, building, city, state, zip, phone, email

  • work_requests:

work_requests_id, customer_id, work_type, description, request_time, completed

我有一个使用此查询显示工作请求的php页面:

  

SELECT * FROM customers,work_requests WHERE customers.customer_id = work_requests.customer_id ORDER BY work_requests_id DESC LIMIT $ startrow,10“

在work_requests表中,'completed'的值为0 or 10 = work not done yet1 = work has been done。我只想显示已完成的工作请求的值为0(即工作尚未完成)。我怎么做?仅供参考,$startrow与分页有关。

4 个答案:

答案 0 :(得分:2)

为什么不添加另一个条件?

SELECT   *
FROM     customers, work_requests 
WHERE    customers.customer_id = work_requests.customer_id 
AND      work_requests.completed = 0
ORDER BY work_requests_id DESC 
LIMIT    $startrow, 10

答案 1 :(得分:0)

因为您没有显示显示这些项目的代码,我认为您的意思是您只想选择已完成的值为0的工作请求

SELECT * 
FROM customers, work_requests 
WHERE customers.customer_id = work_requests.customer_id 
    AND work_requests.completed=0 # add this condition
ORDER BY work_requests_id DESC 
LIMIT $startrow, 10

答案 2 :(得分:0)

您可以尝试:

SELECT * 
FROM customers, work_requests 
WHERE customers.customer_id = work_requests.customer_id AND work_requests.completed=0
ORDER BY work_requests_id DESC LIMIT $startrow, 10

答案 3 :(得分:0)

感谢所有人。 额外的AND条件有效。 我实际上已经尝试过,但是当我测试时它没有用,我急忙还没有将“已完成”的列添加到表中。再次感谢。