mysql - 如何从可变条件的多个表中选择数据| MySQL

标签 mysql sql database select join

我在数据库中有两个表来存储客户基本信息(姓名、位置、电话号码)和另一个表来存储与客户相关的交易(date_sub、profile_sub、isPaid、date_exp、client_id),我有一个 html 表可以查看客户端基本信息和交易(如果可用),我的问题是我无法同时从表 internetClientinternetclientDetails 中查询以选择客户端信息,因为仅当客户端在明细表中有 trans 时才会产生查询。两个表字段如下:

internetClient

--------------------------------------------------------
id         full_name       location    phone_number
-------------------------------------------------------
4         Joe Amine         beirut       03776132
5         Mariam zoue       beirut       03556133

internetclientdetails 

--------------------------------------------------------------------------
incdid   icid      date_sub      date_exp      isPaid      sub_price
----------------------------------------------------------------------------
  6        4      2018-01-01     2018-01-30      0           2000
  7        5      2017-01-01     2017-01-30      0           1000
  8        4      2018-03-01     2018-03-30      1           50000
  9        5      2018-05-01     2019-05-30      1           90000

// incdid > internetClientDetailsId
// icid> internetClientId

如果客户端有 trans in orderdetails,查询应该返回这样的值:

    client_id    full_name           date_sub     date_exp      isPaid    sub_price
-------------------------------------------------------------------------------------
       4          Joe Amine          2018-03-01     2018-03-30      1           50000
       5           Mariam zoue       2018-05-01     2019-05-30      1           90000

否则如果客户端在 internetOrederDetails 中没有 id

    --------------------------------------------------------
    icid      full_name       location    phone_number
    -------------------------------------------------------
    4         Joe Amine         beirut       03776132
    5         Mariam zoue       beirut       0355613

提前致谢

最佳答案

尝试使用左连接。它将显示来自 internetClient 的所有记录以及来自 internetclientdetails 的相关记录

Select internetClient.id, internetClient.full_name
     , internetClient.location, internetClient.phone_number
     , internetclientdetails.incdid, internetclientdetails.icid
     , internetclientdetails.date_sub, internetclientdetails.date_exp
     , internetclientdetails.isPaid, internetclientdetails.sub_price 
from internetClient 
left join internetclientdetails 
  on internetClient.id=internetclientdetails.icid group by internetclientdetails.icid order by internetclientdetails.incdid desc

如果你只想获取付费客户的记录,那么你可以尝试以下

Select internetClient.id, internetClient.full_name
     , internetClient.location, internetClient.phone_number
     , internetclientdetails.icid, internetclientdetails.incdid
     , internetclientdetails.date_sub, internetclientdetails.date_exp
     , internetclientdetails.isPaid, internetclientdetails.sub_price 
from internetClient 
left join internetclientdetails 
  on internetClient.id=internetclientdetails.icid 
 and internetclientdetails.isPaid=1 group by internetclientdetails.icid
order by internetclientdetails.incdid desc

关于mysql - 如何从可变条件的多个表中选择数据| MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50507509/

相关文章:

php - 如何在 Php mySql 中按字母顺序显示表数据?

mysql - 如何为 MySQL 中的多个列指定唯一约束?

mysql - 来自 NDBCLUSTER 的错误 4239 'Trigger with given name already exists'

java - 如何在 MySQL 后端的 Hibernate save() 调用上设置超时?

php - 对 PHP OOP 作用域感到困惑

mysql - spring mvc不会连接到不同计算机上的mysql数据库

ios - 为什么每次访问我的关系对象时都会重新初始化?

database - 如何在 PLSQL Developer 中测试包含 DML 的 Oracle 函数?

sql - 具有不同时间戳的重复条目

明细表中 INSERT 语句的 MySql 约束错误