[英]php mysql select information from three tables from the database
the customer search is carried out by card number (see table A customer) and the code used by me is this:客户搜索是通过卡号进行的(见表A客户),我使用的代码是这样的:
$id = $_POST['card_number'];
SELECT e.*, sum(u.points) as points, sum(u.cost) as cost,
max(date_format(u.date_points, '%e/%c/%Y')) as data
FROM `customer` AS e
INNER JOIN `points` AS u ON e.id_customer = u.id_customer
where card_number='$id'
now I expose my tables and then I explain my problem现在我公开我的表格,然后我解释我的问题
Table A Customer: it contains the information of the members Table A Customer:包含会员信息
id_customer id_customer | name姓名 | card_number卡号 |
---|---|---|
1 1 | Luca卢卡 | 1234567890 1234567890 |
2 2 | Mark标记 | 9876543210 9876543210 |
Table B Points: it contains the points accumulated and the cost incurred Table B Points:包含累积的点数和产生的费用
id_points id_points | id_customer id_customer | points积分 | cost成本 |
---|---|---|---|
1 1 | 1 1 | 5 5 | 20 20 |
2 2 | 2 2 | 10 10 | 40 40 |
3 3 | 1 1 | 20 20 | 60 60 |
4 4 | 2 2 | 35 35 | 35 35 |
Result:结果:
customer with id_customer 1 has a total of 25 points
customer with id_customer 2 has a total of 45 points
Table C deducted points: it contains the points that will be deducted at the next purchase表C 扣除点数:包含下次购买时扣除的点数
id_deducted id_deducted | id_customer id_customer | points积分 |
---|---|---|
1 1 | 1 1 | 5 5 |
2 2 | 2 2 | 15 15 |
Result:结果:
customer with id_customer 1 straight 5 points total remaining 20 points
customer with id_customer 2 climbs 15 points, total remaining 30 points
my question is this how can i make the sum of points from table B - the sum of points from table c?我的问题是,如何计算表 B 中的点总和 - 表 c 中的点总和? how can i add it in the select?我如何将它添加到选择中?
If you want a single request to get all info you need, you can make subrequests like this:如果你想要一个请求来获取你需要的所有信息,你可以像这样发出子请求:
SELECT
c.*,
(
SELECT
SUM(p.points)
FROM
points p
WHERE
p.id_customer = c.id_customer
) AS points,
(
SELECT
SUM(d.points)
FROM
deducted_points d
WHERE
d.id_customer = c.id_customer
) AS deducted_points
FROM
customer c;
The result would be as follows结果如下
id_customer id_customer | name姓名 | card_number卡号 | points积分 | deducted_points扣除点数 |
---|---|---|---|---|
1 1 | Luca卢卡 | 1234567890 1234567890 | 25 25 | 5 5 |
2 2 | Mark标记 | 9876543210 9876543210 | 45 45 | 15 15 |
But it would be very slow on large amounts of users and points changes I think.但是我认为对于大量用户和积分变化来说会非常慢。 So I'd do select from users and then loop through them making individual requests to points
and deducted_points
tables.所以我会从用户中进行选择,然后循环遍历他们,向points
表和deducted_points
表发出单独的请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.