1

I have the SQL to display ALL the activities and relative Admin permissions (if any) for that activity.

Current SQL Code:

SELECT `activities`.*, `admins`.`admin_role_id`
FROM (`activities`)
LEFT JOIN `admins` ON `admins`.`activity_id`=`activities`.`id` AND admins.member_id=27500
WHERE `activities`.`active` =  1

Returning:

id | name | description | active | admin_role_id (or null)

I then need to detect whether they are an active member within that Activity.

I have the following SQL code:

SELECT DISTINCT `products`.`activity_ID` as joinedID
FROM (`transactions_items`)
JOIN `transactions` ON `transactions`.`id` = `transactions_items`.`id`
JOIN `products` ON `products`.`id` = `transactions_items`.`product_id`
JOIN `activities` ON `activities`.`id` = `products`.`activity_ID`
WHERE `transactions`.`member_id` =  27500
AND `activities`.`active` =  1

Is there any way to merge this into one SQL query. I can't figure out how to use the correct JOIN queries, because of the complexity of the JOINs.

Help please, thanks! :)

4

2 回答 2

1

像这样试试

SELECT `activities`.*, `admins`.`admin_role_id`
FROM (`activities`)
LEFT JOIN `admins` ON `admins`.`activity_id`=`activities`.`id` AND admins.member_id=27500
    JOIN (`transactions_items`
    JOIN `transactions` ON `transactions`.`id` = `transactions_items`.`id`
    JOIN `products` ON `products`.`id` = `transactions_items`.`product_id`)
ON `activities`.`id`=`products`.`activity_ID`
WHERE `transactions`.`member_id` =  27500
AND `activities`.`active` =  1
于 2013-07-21T12:04:37.623 回答
0

在我看来,这样的查询会稍微更容易理解,并且(我认为)更符合规范......

SELECT c.*
     , d.admin_role_id
  FROM activities c
  LEFT 
  JOIN admins d
    ON d.activity_id = c.id 
   AND d.member_id = 27500
  LEFT
  JOIN products p
    ON p.activity_ID = c.id 
  LEFT
  JOIN transactions_items ti
    ON ti.product_id = p.id
  LEFT
  JOIN transactions t
    ON t.id = ti.id
   AND t.member_id =  27500
 WHERE c.active =  1
于 2013-07-21T13:41:16.540 回答