简体   繁体   English

PHP MySQL JOIN查询问题

[英]PHP MySQL JOIN Query Issues

This one has me pretty confused and after reading numerous forums and "how to" sites, I think I'm even more confused why this isn't working. 这个让我非常困惑,在阅读了许多论坛和“操作方法”网站之后,我认为我什至更加困惑为什么它不起作用。

I have 2 tables that I am joining. 我要加入2张桌子。

Table 1 (products) contains all my product details. 表1(产品)包含我所有的产品详细信息。

Table 2 (ratings) contains all the ratings for each product and each product has 3 ratings from 3 different rating entities. 表2(等级)包含每种产品的所有等级,每种产品具有3个不同等级实体的3个等级。

I am working on a set of AJAX search filters and need to write the query in a manner that incorporates all of the filters. 我正在处理一组AJAX搜索过滤器,需要以合并所有过滤器的方式编写查询。

The filters include price, number of reviews, average review stars, rating 1, rating 2, rating 3. 过滤器包括价格,评论数量,平均评论星级,等级1,等级2,等级3。

The price, num reviews, and avg stars comes from Table 1 (products). 价格,数量评论和平均星数来自表1(产品)。

Rating 1, 2, and 3 come from Table 2 (ratings). 等级1、2和3来自表2(等级)。

I need something like this for my query, but this is returning zero results when I know (have verified in the database) that I have records that meet the criteria I'm querying for. 我的查询需要这样的东西,但是当我知道(已在数据库中验证)我的记录符合我要查询的条件时,这将返回零结果。

SELECT p.id, p.name 
FROM products p 
JOIN ratings r ON p.id = r.id 
WHERE p.num_reviews BETWEEN '0' AND '1000' 
AND p.price BETWEEN '0' AND '500' 
AND p.avg_rvw_stars BETWEEN '0' AND '5' 
AND (r.ratingSource='1' AND (r.rating BETWEEN '1' AND '4')) 
AND (r.ratingSource='2' AND (r.rating BETWEEN '0' AND '100')) 
AND (r.ratingSource='3' AND (r.rating BETWEEN '0' AND '100')) 
ORDER BY p.name ASC

The issue seems to reside in the last three WHERE statements. 问题似乎出在最后三个WHERE语句中。 If I remove any 2 of the 3, I get results. 如果我删除3中的任何2个,就会得到结果。 But when I include all 3, I get nothing when there are records that qualify. 但是,当我将所有3个都包括在内时,如果有符合条件的记录,我将一无所获。

If I change the last two AND's to OR, I also get results, but it's not the result set that is expected or needed. 如果将最后两个AND更改为OR,我也会得到结果,但这不是预期或需要的结果集。

Am I not writing this correctly? 我写的不正确吗?

Any help/guidance is greatly appreciated!! 任何帮助/指导都将不胜感激!

Your problem is that you confuse the meaning of AND and OR . 您的问题是您混淆ANDOR的含义。

You are looking for things that have, for instance 您正在寻找的东西,例如

r.ratingSource='1'

and a bit later they ALSO should have 一会儿他们也应该有

r.ratingSource='2'

There is NOTHING that will be valid for that, because it is a single thing that can NOT be both of them. 没有什么是对这有效的,因为这是一件事情,不能兼而有之。 So you need to think about what AND does. 因此,您需要考虑AND作用。

If you have some stuff EACH result must have, you should use AND. 如果您有每个结果都必须具备的内容,则应使用AND。 If you have groups of stuff they might have, you can do (thing AND otherthing) OR (thing2 and otherthing2) 如果您有一组他们可能拥有的东西,则可以执行(thing AND otherthing) OR (thing2 and otherthing2)

It's all basic logic really :) 这实际上是所有基本逻辑:)

I guess you probably mean this: 我想你可能是这个意思:

SELECT p.id, p.name 
FROM products p 
JOIN ratings r ON p.id = r.id 
WHERE p.num_reviews BETWEEN '0' AND '1000' 
AND p.price BETWEEN '0' AND '500' 
AND p.avg_rvw_stars BETWEEN '0' AND '5' 
AND (
        (r.ratingSource='1' AND (r.rating BETWEEN '1' AND '4')) 
     OR (r.ratingSource='2' AND (r.rating BETWEEN '0' AND '100')) 
     OR (r.ratingSource='3' AND (r.rating BETWEEN '0' AND '100'))
) 
ORDER BY p.name ASC

If you have 3 different rows with the ratings, you can't use that logic. 如果您的评分有3个不同的行,则不能使用该逻辑。 Each row is checked for the WHERE conditions once, so there cannot be a row with a rating both 1 and 2 . 每行都要检查一次WHERE条件,因此不能有评级均为12

The easiest way is to join thrice: 最简单的方法是加入三次:

SELECT p.id, p.name 
FROM products p 
  JOIN ratings r1 ON  p.id = r1.id 
                  AND r1.ratingSource = 1
                  AND r1.rating BETWEEN 1 AND 4
  JOIN ratings r2 ON  p.id = r2.id 
                  AND r2.ratingSource = 2
                  AND r2.rating BETWEEN 0 AND 100
  JOIN ratings r3 ON  p.id = r3.id
                  AND r3.ratingSource = 3
                  AND r3.rating BETWEEN 0 AND 100
WHERE p.num_reviews BETWEEN 0 AND 1000 
  AND p.price BETWEEN 0 AND 500 
  AND p.avg_rvw_stars BETWEEN 0 AND 5 
ORDER BY p.name ASC ;

I think you need 我觉得你需要

AND 
(
     (r.ratingSource='1' AND (r.rating BETWEEN '1' AND '4')) 
      OR (r.ratingSource='2' AND (r.rating BETWEEN '0' AND '100')) 
      OR (r.ratingSource='3' AND (r.rating BETWEEN '0' AND '100')) 
    )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM