Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySql查询按另一个表中的存在排序_Mysql_Sql - Fatal编程技术网

MySql查询按另一个表中的存在排序

MySql查询按另一个表中的存在排序,mysql,sql,Mysql,Sql,我有三张桌子(产品、产品信息和特价) product_id是引用产品id的外键 搜索产品时,我想按特价产品订购此搜索 这是我的尝试 SELECT products.*, product_info.* FROM products INNER JOIN product_info ON product_info.product_id = products.id INNER JOIN specials ON specials.product_d = products.id WHERE product_i

我有三张桌子(产品、产品信息和特价)

product_id是引用产品id的外键

搜索产品时,我想按特价产品订购此搜索

这是我的尝试

SELECT products.*, product_info.* FROM products
INNER JOIN product_info ON product_info.product_id = products.id
INNER JOIN specials ON specials.product_d = products.id
WHERE product_info.language = 'en'
AND product_info.title like ?
AND specials.from_date < NOW()
AND specials.to_date > NOW()
ORDER BY specials.product_id DESC, products.created_at DESC
从产品中选择产品。*,产品信息。*
内部连接product\u info ON product\u info.product\u id=products.id
specials.product\u d=products.id上的内部联接特殊
其中product_info.language='en'
产品名称是什么?
和特价商品。起始日期<现在()
和特价商品。截止日期>现在()
按specials.product\u id描述、products.created\u描述订购
但结果只会是特殊产品。

试试这个

SELECT products.*, product_info.* FROM products
INNER JOIN product_info ON product_info.product_id = products.id
INNER JOIN specials ON specials.product_d = products.id
WHERE product_info.language = 'en'
AND product_info.title like ?
AND specials.from_date < NOW()
AND specials.to_date > NOW()
ORDER BY specials.product_id DESC,products.id DESC, products.created_at DESC
从产品中选择产品。*,产品信息。*
内部连接product\u info ON product\u info.product\u id=products.id
specials.product\u d=products.id上的内部联接特殊
其中product_info.language='en'
产品名称是什么?
和特价商品。起始日期<现在()
和特价商品。截止日期>现在()
按specials.product\u id DESC、products.id DESC、products.created\u at DESC订购

如果不是每个产品都在specials表中,您应该改为使用specials进行左连接,并将specials日期的验证放在ON子句中。然后您可以按products.id订购,但首先要将特价商品放在首位,方法是在以下情况下使用案例进行验证:

SELECT products.*, product_info.*
FROM products
INNER JOIN product_info ON product_info.product_id = products.id
LEFT JOIN specials ON specials.product_d = products.id
                  AND specials.from_date < NOW()
                  AND specials.to_date > NOW()
WHERE product_info.LANGUAGE = 'en'
  AND product_info.title LIKE ?
ORDER BY CASE 
    WHEN specials.product_id IS NOT NULL THEN 2
    ELSE 1
    END DESC,
  products.id DESC,
  products.created_at DESC
选择产品。*,产品信息*
来自产品
内部连接product\u info ON product\u info.product\u id=products.id
在specials.product\u d=products.id上左键连接specials
和特价商品。起始日期<现在()
和特价商品。截止日期>现在()
其中product_info.LANGUAGE='en'
产品名称是什么?
逐案订购
当specials.product_id不为空时,则为2
其他1
结束描述,
products.id DESC,
在描述处创建的products.u

由于没有所有产品的特价,因此必须进行外部连接而不是内部连接。

否与以前的结果相同
SELECT products.*, product_info.*
FROM products
INNER JOIN product_info ON product_info.product_id = products.id
LEFT JOIN specials ON specials.product_d = products.id
                  AND specials.from_date < NOW()
                  AND specials.to_date > NOW()
WHERE product_info.LANGUAGE = 'en'
  AND product_info.title LIKE ?
ORDER BY CASE 
    WHEN specials.product_id IS NOT NULL THEN 2
    ELSE 1
    END DESC,
  products.id DESC,
  products.created_at DESC
SELECT 
   products.*, 
   product_info.*
FROM products
JOIN product_info ON product_info.product_id = products.id
LEFT JOIN specials 
   ON specials.product_d = products.id AND NOW() BETWEEN specials.from_date AND specials.to_date
WHERE product_info.LANGUAGE = 'en'
  AND product_info.title LIKE ?
ORDER BY 
   specials.product_id DESC NULLS LAST,
   products.product_id DESC,
   products.created_at DESC;