php - 使用多个表在 mysql 中进行多个联合和联接

标签 php mysql sql

我在 mysql 查询中创建了 UNION。这对我来说效果很好。

此查询的主要目的是从多个表中获取通知的数据。

这需要 0.0009 秒 时间来执行。数据较少。

任何sql专家可以帮助我改进这个sql查询或者指导我这个sql查询吗?

我不太擅长mysql

  SELECT
        HM_notification.product_id,
        HM_products.slug,
        HM_notification.action_type,
        HM_notification.url,
        HM_notification.read_date
        FROM `HM_notification` 
            JOIN HM_customers ON HM_customers.id = HM_notification.added_by 
            JOIN HM_products ON HM_products.id = HM_notification.product_id 
            JOIN HM_reviews ON HM_reviews.product_id = HM_notification.product_id 
            WHERE HM_products.user_id=34 AND HM_notification.added_by != 34 AND action_type = 'reviews'

    UNION        

  SELECT 
        HM_notification.product_id,
        HM_products.slug,
        HM_notification.action_type,
        HM_notification.url,
        HM_notification.read_date
        FROM `HM_notification` 
            JOIN HM_customers ON HM_customers.id = HM_notification.added_by 
            JOIN HM_bid ON HM_bid.product_id = HM_notification.product_id 
            JOIN HM_products ON HM_products.id = HM_notification.product_id 
            WHERE HM_products.user_id=34 AND HM_notification.added_by != 34  AND action_type = 'newbid' OR action_type = 'editbid'

    UNION        

    SELECT 
        HM_notification.product_id,
        HM_products.slug,
        HM_notification.action_type,
        HM_notification.url,
        HM_notification.read_date
        FROM `HM_notification` 
            JOIN HM_customers ON HM_customers.id = HM_notification.added_by 
            JOIN HM_products ON HM_products.id = HM_notification.product_id 
            JOIN HM_order_items ON HM_order_items.product_id = HM_notification.product_id 
            WHERE HM_products.user_id=34 AND HM_notification.added_by != 34 AND (action_type = 'order_pending' OR action_type = 'processing' OR action_type = 'onhold' OR action_type = 'unhold' OR action_type = 'receive' OR action_type = 'addshipment' OR action_type = 'editshipment' OR action_type = 'newcomment' OR action_type = 'addshipmentcomment')

    UNION        

  SELECT 
        HM_notification.product_id,
        HM_products.slug,
        HM_notification.action_type,
        HM_notification.url,
        HM_notification.read_date
        FROM `HM_notification` 
            JOIN HM_customers ON HM_customers.id = HM_notification.added_by 
            JOIN HM_products ON HM_products.id = HM_notification.product_id 
            JOIN HM_conversation ON HM_conversation.pid = HM_notification.product_id 
            WHERE HM_products.user_id=34 AND HM_notification.added_by != 34 AND (HM_conversation.user_one != 34 || HM_conversation.user_two != 34) AND action_type = 'conversation'

这是一个简单的 SQL 查询,用于获取带有产品 ID 的特定 url。

最佳答案

因为我还没有看到你的表,所以我现在的想法是,如果可能的话,你最好使用“NATURAL JOIN”而不是“JOIN”,并将条件放在查询的“WHERE”部分。因为前者更快、更明智......

希望有帮助...

关于php - 使用多个表在 mysql 中进行多个联合和联接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27657394/

相关文章:

PHP 检测(或删除)当前驱动器号?

PHP 将 2 个值存储到 1 个数组索引中

php - MySQL 大请求在 AJAX 中不起作用,需要 LIMIT,而在直接 PHP 中工作

mysql - "Where"基于连接结果的语句

php - 如何将 solr 与 mysql 和 php 一起使用?

php - 从 CodeIgniter 中每个页面所需的模型中检索数据的最佳方法是什么?

PHP 全文搜索不起作用

php file_get_contents 向 Bugzilla 安装发送错误的内容类型

c# - 减少 Update 和 Sum Query C# Access DB 的时间?

sql - 将 ruby​​ 与数据库一起使用,但没有 rails