如果左边连接在其他表格中不匹配,请选择值

时间:2017-06-27 10:54:50

标签: php mysql mysqli

我在从数据库的两个表中获取值时遇到了一些问题。 我的数据库中有两个表1是mem,第二个是付款 mem存储用户的名称和drawid 支付表存储用户的提取和分期付款

用户每月向我们付款。 因此,如果绘图ID为1的用户付费给我们,则两个表中的值为 mem drawid = 1和name = something payment draw = 1 and installment = 2

mem中的drawid与付款中的draw相同

所以这些表有很多关系。 现在我需要找到所有在4个月之前没有支付1个分期付款的会员名单。

我正在使用此查询

 public static CustomDate getYrMonthDateFormateString(String datestring) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss",Locale.getDefault());
    Date d = sdf.parse(datestring);
    return new CustomDate(d);
 }

 public static class CustomDate {

    final String dayOfTheWeek;
    final String day;
    final String monthString;
    final String monthStringComplete;
    final String monthNumber;
    final String year;
    final String time;

    CustomDate(Date date) {

      dayOfTheWeek = (String) DateFormat.format("EEE", date); // Thursday
      day = (String) DateFormat.format("dd", date); // 20
      monthString = (String) DateFormat.format("MMM", date); // Jun
      monthStringComplete = (String) DateFormat.format("MMMM", date); // June
      monthNumber = (String) DateFormat.format("MM", date); // 06
      year = (String) DateFormat.format("yy", date); // 2013
      time = ((String) DateFormat.format("h:mm a", date)).toLowerCase().replace(" ", ""); // 6:00 am
    }

    public String getDayOfTheWeek() {
      return dayOfTheWeek;
    }

    public String getDay() {
      return day;
    }

    public String getMonthString() {
      return monthString;
    }

    public String getMonthNumber() {
      return monthNumber;
    }

    public String getYear() {
      return year;
    }

    public String getTime() {
      return time;
    }

    public String getMonthStringComplete() {
      return monthStringComplete;
    }

 }

它运作良好没有问题只有问题是我还需要显示用户付款的分期付款所以我需要从表格付款中获取所有分期付款,然后通过while循环显示。

这个查询很完美,但它给了我重复的结果!!!

 SELECT drawid,contact,dnd,mem.name, count(*) as numPayments FROM mem 
 LEFT JOIN payment ON (mem.drawid = payment.draw) GROUP BY 
 drawid HAVING numPayments < 4

2 个答案:

答案 0 :(得分:1)

    SELECT drawid,contact,dnd,mem.name, count(*) as numPayments,NULL numPaidPayments ,NULL PAID_CONTACT,NULL NAME_PAID FROM mem 
    LEFT JOIN payment ON (mem.drawid = payment.draw) GROUP BY 
    drawid HAVING numPayments < 4
    UNION
    SELECT NULL drawid,NULL contact, NULL dnd, NULL name,NULL numPayments,COUNT(*) as numPaidPayments ,contact PAID_CONTACT,mem.name NAME_PAID  FROM mem 
    INNER JOIN payment ON (mem.drawid = payment.draw) GROUP BY 
    drawid HAVING numPaidPayments >= 4

尝试以上查询。

希望这会对你有所帮助。

答案 1 :(得分:0)

鉴于此

MariaDB [sandbox]> select * from member;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
+------+
4 rows in set (0.00 sec)

MariaDB [sandbox]> select * from payment;
+--------+------+
| mem_id | mth  |
+--------+------+
|      1 |    1 |
|      1 |    2 |
|      1 |    3 |
|      1 |    4 |
|      2 |    1 |
|      2 |    3 |
|      3 |    2 |
|      3 |    4 |
+--------+------+
8 rows in set (0.00 sec)

这个

select m.id, group_concat(p.mth order by p.mth) mthspaid,
        sum(case when p.mem_id is not null then 1 else 0 end) NoofMthsPaid,
        4 - sum(case when p.mem_id is not null then 1 else 0 end) NoofMthsMissed
from member m
left join payment p on p.mem_id = m.id
group by m.id

给出这个

+------+----------+--------------+----------------+
| id   | mthspaid | NoofMthsPaid | NoofMthsMissed |
+------+----------+--------------+----------------+
|    1 | 1,2,3,4  |            4 |              0 |
|    2 | 1,3      |            2 |              2 |
|    3 | 2,4      |            2 |              2 |
|    4 | NULL     |            0 |              4 |
+------+----------+--------------+----------------+
4 rows in set (0.00 sec)

如果您添加代码以计算到期月份

select paid.*, due.mthsmissed
from
(
select m.id, group_concat(p.mth order by p.mth) mthspaid,
        sum(case when p.mem_id is not null then 1 else 0 end) NoofMthsPaid,
        4 - sum(case when p.mem_id is not null then 1 else 0 end) NoofMthsMissed
from member m
left join payment p on p.mem_id = m.id
group by m.id
) paid
left join
(    
select due.id, group_concat(due.mth order by due.mth) MthsMIssed
from
(        
select m.id,d.mth
from member m,(select 1 mth union select 2 union select 3 union select 4) d
) due
left join payment p on p.mem_id = due.id and p.mth = due.mth
where p.mth is null
group by due.id
) due on paid.id = due.id 

你得到了这个

+------+----------+--------------+----------------+------------+
| id   | mthspaid | NoofMthsPaid | NoofMthsMissed | mthsmissed |
+------+----------+--------------+----------------+------------+
|    1 | 1,2,3,4  |            4 |              0 | NULL       |
|    2 | 1,3      |            2 |              2 | 2,4        |
|    3 | 2,4      |            2 |              2 | 1,3        |
|    4 | NULL     |            0 |              4 | 1,2,3,4    |
+------+----------+--------------+----------------+------------+
4 rows in set (0.04 sec)