简体   繁体   English

自我连接还是替代的MySQL子查询?

[英]MySQL subquery within self join or alternative?

I'm trying to form a query for our scholarship fundraising club that returns all ticket sales to a dinner event, as well as indicates whether or not each member who's purchased tickets is current on her dues (paid within last 365 days). 我正在尝试对我们的奖学金筹款俱乐部进行查询,该查询将所有门票销售返还给晚餐活动,并指出是否已购买门票的每个会员都使用她的会费(过去365天内支付)。 This will be used for a sign-in sheet that indicates how many tickets have been purchased and whether we need to ask them to renew their membership yet, update their contact info, etc. I can return either ticket sales or active memberships, but I can't figure how to incorporate both queries into a single query so I won't have to use a PHP array. 这将用于登录表,该表指示已购买了多少张票证以及我们是否需要让他们更新其会员资格,更新其联系信息等。我可以退回票证销售或有效的会员资格,但是我可以无法弄清楚如何将两个查询合并到一个查询中,因此我不必使用PHP数组。 Hopefully someone can help: 希望有人可以提供帮助:

/* Here are excerpts from the relevant tables */ / *以下是相关表格的摘录* /

+----------+  +--------------------+  +--------------------------+
| members  |  | store_transactions |  | store_product_categories |
+----------+  +--------------------+  +--------------------------+
| id       |  | id                 |  | id                       |
| last     |  | invoice            |  | category                 |
| first    |  | memberID           |  | officeID                 |
| spouse   |  | categoryID         |  | modified                 |
| lifetime |  | productID          |  +--------------------------+
| email    |  | name               |  
| phone    |  | price              |  +-------------+
| created  |  | quantity           |  | pmt_methods |
+----------+  | addedBy            |  +-------------+
              | pmtMethod          |  | id          |
              | created            |  | method      |
              +--------------------+  +-------------+

/* The 1st query returns all Muster ticket sales w/whom added (auto transaction via website or manual via club officer) and method of pmt (RSVP only, cash, etc) */ / *第一个查询返回添加的所有Muster机票销售(通过网站进行自动交易或通过俱乐部官员进行手动交易)和PMT方法(仅RSVP,现金等)* /

SELECT member.last, member.first, member.spouse,  
       member.email, member.phone, 
       SUM(transaction.quantity) dinnerTix, 
       SUM(transaction.price * transaction.quantity) total, 
       IF (addedBy = '0' OR addedBy IS NULL, 'Website', 
           CONCAT_WS(' ', officer.first, officer.last)) 
           AS addedBy, method
FROM store_transactions AS transaction
LEFT JOIN members AS member ON transaction.memberID = member.id
LEFT JOIN store_product_categories ON transaction.categoryID =   
          store_product_categories.id 
LEFT JOIN pmt_methods ON transaction.pmtMethod = pmt_methods.id
LEFT JOIN members officer ON officer.id = addedBy
WHERE categoryID = '2' 
    AND year(transaction.created) = '2015' 
    AND (name LIKE '%member%') 
GROUP BY CONCAT(member.last, '_', member.first)
ORDER BY member.last, member.first;

The 1st query returns something like this: 第一个查询返回如下内容:

+------+-------+--------+-------------+--------------+---
| last | first | spouse | email       | phone        |
+------+-------+--------+-------------+--------------+---
|  Doe | John  | Jane   | abc@abc.com | 123-456-7890 |
+------+-------+--------+-------------+--------------+---

---+-----------+--------+-----------+--------+
   | dinnerTix | total  | addedBy   | method |
---+-----------+--------+-----------+--------+
   |     5     | 150.00 | President | cash   |
---+-----------+--------+-----------+--------+

/* This 2nd query returns all active members, adding 1 year to last membership purchase to show annual membership expiration */ / *此第二个查询返回所有活动成员,在上一次会员购买时加1年以显示年度会员到期* /

SELECT last, first,
    DATE_FORMAT(ADDDATE(store_transactions.created, INTERVAL 1 YEAR), 
        '%M %e, %Y') AS expires
FROM store_transactions
LEFT JOIN members ON store_transactions.memberID = members.id
WHERE store_transactions.created >= SUBDATE(CURDATE(), INTERVAL 1     
    YEAR) AND categoryID = 1 OR lifetime = 'Y'
GROUP BY members.id
ORDER BY members.last, members.first;

The 2nd query returns something like this: 第二个查询返回如下内容:

+------+-------+-------------+
| last | first | expires     |
+------+-------+-------------+
|  Doe | John  | May 3, 2015 |
+------+-------+-------------+

I thought it would be simple to combine the two queries so the expiration date returned in the second query would be appended to each row of the first query result. 我认为合并两个查询将很简单,因此第二个查询中返回的到期日期将附加到第一个查询结果的每一行。 Unfortunately, I'm chasing my tail here, so any help would be greatly appreciated! 不幸的是,我在这里追尾,所以任何帮助将不胜感激! This was my first foray into self-joins and table aliases, so I'm trying to expand my horizons without breaking the site which has been operating smoothly for the last 8 years! 这是我第一次尝试使用自联接和表别名,因此我试图在不破坏过去8年运行良好的站点的情况下扩大视野! Thanks!! 谢谢!!

It should be easy. 应该很容易。 Lets say Q1 = your first query, and Q2 = your second query. 假设Q1 =您的第一个查询,而Q2 =您的第二个查询。 it should be as simple as: 它应该很简单:

select q1.*, q2.expires
  from (Q1) q1
    inner join (Q2) q2
      on q1.last = q2.last and q1.first = q2.first

Simply copy and paste your first query where Q1 is, inside the parenthesis, and do the same for your second query into Q2 只需复制和粘贴您的第一个查询,其中Q1是,括号里面,做同样的你的第二个查询到Q2

Of course, this will break if there are two people with the same name, in which case you would be better off including the id field in both queries, and joining on that. 当然,如果有两个同名的人,这会中断,在这种情况下,最好在两个查询中都包含id字段,然后再加入。

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

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