我在mysql查询中创建了一个UNION。这对我来说很好。
此查询的主要目的是从多个表中获取通知的数据。
这需要0.0009 sec
次执行。数据较少。
任何sql专家都可以帮我改进这个SQL查询或引导我进行这个SQL查询。
I am not good in 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的特定网址。
答案 0 :(得分:1)
因为我没有看到你的桌子,我现在的想法是,你最好使用' NATURAL JOIN'而不是加入'如果可能的话,把你的条件放在' WHERE'部分查询。因为前者更快更聪明......
希望它有所帮助...答案 1 :(得分:0)
使用此查询。
SELECT
n.product_id,
p.slug,
(case when (p.user_id=9 and n.action_type='product_review') then 'reviews'
when (n.action_type='bid_added') then p.slug
when (n.action_type='order_processing' OR n.action_type='order_shipped' OR n.action_type='order_onhold' OR n.action_type='order_unhold' OR n.action_type='received') then 'orderlist'
when ((con.user_one!=9 OR con.user_two!=9) and n.action_type='chat') then 'messages' end) HM_url
from HM_notification n,
HM_customers c,
HM_products p,
HM_reviews r,
HM_bid b,
HM_order_items o,
HM_conversation con
where
n.added_by=c.id
and p.id=n.product_id
and r.product_id=n.product_id
and b.product_id=n.product_id
and o.product_id=n.product_id
and con.pid=n.product_id
and n.added_by != 9;
答案 2 :(得分:0)
试试这个:
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 OR HM_conversation.user_two != 34) AND action_type = 'conversation'