Hi i have a two tables and i want to fetch data, one table have agent_id and 2nd table has agent_id, and agent_sale record so, i just want to show data with join query so that i can get not repeat user_id with multiple sales record according to sales.
1st table - agent_sales_daily
id agent_id agent_username userlevel status date
1 2 rajesh 2 1 2019-09-04
2 3 ram 2 1 2019-09-04
4 4 amit 2 1 2019-09-04
2st table - agent_sale
id agent_id agent_sale status date
1 2 300 1 2019-09-04
2 3 500 1 2019-09-04
3 3 200 1 2019-09-04
4 4 0 1 2019-09-04
5 3 600.55 1 2019-09-05
7 3 300.50 1 2019-09-05
i am trying like this
SELECT b.agent_username,a.agent_sale,b.agent_id,a.date
FROM agent_sale a
left join agent_sales_daily b ON a.agent_id = b.agent_id
$sqlQ ="SELECT b.agent_username,a.agent_sale,b.agent_id,a.date
FROM $sale a
left join $table b ON a.agent_id = b.agent_id
$res = $conn->query($sqlQ);
i want output put is like 1 agent_username have multiple sales and i want show record like ram has 3 sales and show one ram and three sales record but not duplicate ram
thanks in advance
Related
I have read many topics but none have given me exactly what i need
I have 3 tables
products
id
product_name
1
Heineken
2
Budweiser
transaction
id
user_id
date
amount
32
4
01/23/2023
10000
45
2
01/23/2023
20000
57
4
01/23/2023
5000
transaction_details
id
transaction_id
product_id
1
32
1
2
32
2
3
45
2
4
45
1
5
57
1
Now how can i achieve this
SELECT FROM transaction WHERE user_id = 4 then use the results to SELECT FROM transaction_details and display it in a table using a single sql query
And when displaying the results from transaction_details i want to use product_name
This is the result i want
transaction_id
products_id and name
32
1. Heineken
32
2. Budweiser
57
1. Heineken
I have tried to select from transaction table where id is equal to 4 and i got two results, i don't know what to do next
Using Mysqli Join Query with select concat option in in your select query
Please Using This Way
select tran.id,CONCAT(pro.id,'.',pro.product_name) as products_id_and_name from transaction as tran join transaction_details as trand on tran.id = trand.transaction_id JOIN products as pro on pro.id = trand.product_id WHERE tran.user_id = 4;
the customer search is carried out by card number (see table A customer) and the code used by me is this:
$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
id_customer
name
card_number
1
Luca
1234567890
2
Mark
9876543210
Table B Points:
it contains the points accumulated and the cost incurred
id_points
id_customer
points
cost
1
1
5
20
2
2
10
40
3
1
20
60
4
2
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
id_deducted
id_customer
points
1
1
5
2
2
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? 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
name
card_number
points
deducted_points
1
Luca
1234567890
25
5
2
Mark
9876543210
45
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.
I'm working with PHP and MySql. I'm trying to find a way to select a number of movies from a mysql table, but apart from the movies table I have a watchlist table that stores a userID and the movieID of the movies he/she has added to his/her watchlist:
id userID movieID
=====================================
1 1 3
2 1 5
3 1 7
4 2 3
5 2 2
6 3 2
The movies table looks something like this
movieID title duration
=============================
1 tit1 34:43
2 tit2 35:43
3 tit3 24:43
4 tit4 34:13
5 tit5 11:43
6 tit6 22:43
7 tit7 33:43
The result I'm after is (for example for the user with ID 1):
movieID title duration added
=======================================
1 tit1 34:43 false
2 tit2 35:43 false
3 tit3 24:43 true
4 tit4 34:13 false
5 tit5 11:43 true
6 tit6 22:43 false
7 tit7 33:43 true
Is there a way to join both the movies and the watchlist table to produce the desired result?
Thanks.
You can get the required output by using a LEFT JOIN and then checking for a NULL in the join table.
SELECT m.*, IF(w.id IS NULL, 0, 1) AS added
FROM movies m
LEFT JOIN watchlist w ON (m.movieID = w.movieID AND w.userID = 1)
GROUP BY m.movieID
I have a little problem to solve, but I can't. I have the following chat table:
id user friend msg date
----------------------------------------------------------------------
1 1 2 Hello Bob! 2014-07-04 01:00
2 1 2 How are you doing? 2014-07-04 01:01
3 2 1 I'm fine bro! 2014-07-04 02:30
4 1 3 Hey Mark :D 2014-07-04 02:31
5 3 1 Yo! 2014-07-04 02:32
6 4 1 Wassup?! 2014-07-04 07:00
I'm working on a PHP getInbox($uid) method that returns an array of the last message of each pair (user and friend). I tried a SELECT query with GROUP BY friend, but it is incomplete.
SELECT * FROM `chat`
GROUP BY `friend`
WHERE `user` = $uid OR `friend` = $uid
The desired result is:
id user friend msg date
----------------------------------------------------------------------
3 2 1 I'm fine bro! 2014-07-04 02:30
5 3 1 Yo! 2014-07-04 02:32
6 4 1 Wassup?! 2014-07-04 07:00
I would appreciate a help!
SELECT c.* FROM
chat c
JOIN
(SELECT
max(id) max_id,
(CASE WHEN user < friend THEN user ELSE friend END) user_a,
(CASE WHEN user < friend THEN friend ELSE user END) user_b
FROM chat
GROUP BY user_a, user_b) t1 ON t1.max_id = c.id
The case statements select (user,friend) in ascending order. For example, both (1,2) and (2,1) will be converted to (1,2). An ordered pair uniquely identifies a conversation. Finally, the latest id is selected for each ordered pair and rows having those ids are displayed from the chat table.
Im having some trouble with this.
Here's an example of my tables.
Booking
id zone_id name excursion_id
1 2 1
2 1 1
3 2 1
The table where I have the quantities
booking_price
id_booking id_price quantity
1 1 2
1 2 3
2 1 1
2 2 0
3 1 2
3 2 3
Here the zone table
Zone
id Name
1 a
2 b
3 c
So I want to have a table like that
Zone_id Quantity
1 1
2 10
3 0
The problem is when im joining tables and filtering by excursion_id im not getting ALL the zones.
I want to know how many people goes in each zone.
I think better way of doing it is
select z.id, coalesce(sum(bp.quantity),0) as quantity
from Booking b
right join Zone z on z.id = b.zone_id AND b.excursion_id = 1
left join booking_price bp on bp.id_booking = b.id
group by z.id
demo http://sqlfiddle.com/#!2/771f5/13
You could use a query like:
SELECT zone.id as zone_id,
sum(quantity) as Quantity
FROM zone
LEFT JOIN Booking on Booking.zone_id = zone.id
LEFT JOIN Excursion on Excursion.id_booking = Booking.id
GROUP BY zone.id