php - MYSQL - 从匹配的两个表中获取值

标签 php mysql

我正在尝试从匹配的两个表中获取值。

预订表:

 id, member_id, salon_id, service_id, appointment_date, booking_date
  1,         5,        2,          1,       2015-08-29,   2015-08-29
  2,         5,        2,          2,       2015-08-29,   2015-08-29
  3,         5,        3,          3,       2015-08-29,   2015-08-29
  4,         5,        3,          4,       2015-08-29,   2015-08-29

服务表:

 service_id, salon_id, service_name, price
          1,        2, Make-Up     , 25
          2,        2, Hair Styling, 10
          3,        3, Waxing      , 15
          4,        3, Threading   , 20

当前查询:

SELECT b.salon_id 
     , s.service_id
     , s.price
     , b.booking_date 
  FROM bookings b
  JOIN services s
    ON b.salon_id = s.salon_id 
   AND b.salon_id = 2 
   AND b.booking_date = '2015-08-29';

当前结果:

  salon_id, service_id, price, booking_date
         2,          1,    25,   2015-08-29
         2,          1,    25,   2015-08-29
         2,          2,    10,   2015-08-29
         2,          2,    10,   2015-08-29

预期结果:

 salon_id, service_id, price, booking_date
        2,          1,    25, 2015-08-29
        2,          2,    10, 2015-08-29

我想做的是。我想要特定日期的所有特定沙龙预订。之后我将对价格求和以获得特定日期沙龙的总销售额。

有什么帮助吗?

提前致谢。

最佳答案

试试 DISTINCT:

SELECT DISTINCT bookings.salon_id, services.service_id, services.price,
bookings.booking_date FROM bookings INNER JOIN services ON
bookings.salon_id=services.salon_id AND bookings.salon_id = 2 AND
bookings.booking_date = '2015-08-29';

关于php - MYSQL - 从匹配的两个表中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32287154/

相关文章:

php - 法语、俄语等外语的拼写纠正

php - Symfony5 项目的 Phan/phan 配置

mysql - SQL : select top 4 results with the higher votes UP and lower votes DOWN

mysql - 如何使用 xslt 将默认的 mysql modtimes 转换为 UTC 日期时间

mysql - SQL根据多列删除同一个表中的重复行

php - 如何在 MySQL 数据库中获取枚举可能值?

php - db 获取数组 drupal 7

javascript - 在 PHP 中向 Body 添加一个类并将其与 session 一起保存

php - Mysql与LIMIT函数的混淆

MySQL 内连接?