简体   繁体   English

MYSQL - 在匹配时从两个表中获取值

[英]MYSQL - Getting Values From Two Tables on Match

I am trying to get a value from two tables on match. 我想从匹配中的两个表中获取一个值。

Booking Table: 预订表:

 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

Services Table: 服务表:

 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

Current Query: 当前查询:

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';

Current Result: 目前的结果:

  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

Expected Result: 预期结果:

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

What i am trying to do is. 我想做的是。 I want all the specific salon booking for specific date. 我想要具体日期的所有特定沙龙预订。 After that i will sum price to get total sales of salon for specific date. 之后我将总结价格以获得特定日期沙龙的总销售额。

Any help please? 有什么帮助吗?

Thanks in advance. 提前致谢。

Try DISTINCT : 尝试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';

I think you also need a condition on the services id in the join: 我认为你还需要加入服务id的条件:

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

Also notice how table aliases make the query easier to write and to read. 还要注意表别名如何使查询更容易编写和读取。

Try this query: 试试这个查询:

SELECT bookings.salon_id, services.service_id, services.price,bookings.booking_date 
FROM bookings 
LEFT JOIN services ON bookings.salon_id=services.salon_id AND bookings.service_id = services.service_id;

This query will display data from booking that exist in service. 此查询将显示服务中存在的预订数据。

bookings.salon_id=services.salon_id AND bookings.salon_id = 2 AND bookings.booking_date = '2015-08-29' ; bookings.salon_id = services.salon_id AND bookings.salon_id = 2 AND bookings.booking_date ='2015-08-29' ;

make your query not dynamic. 使您的查询不动态。 How if you want to get salon_id 1 or 2 or 3 or all of them? 如果你想获得salon_id 1或2或3或全部?

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

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