I have 2 tables
fist one customer
id, name
1, Adam
2, Sam
3, Erik
second one orders
id, father_id , order
1, 1, 1000
2, 1, 2000
4, 2, 4000
5, 3, 600
6, 3 , 433
the php output should be
Adam : orders : 1 - 1000 , 2 - 2000
Sam : orders : 4 - 4000
Erik : orders : 5 - 300 , 6 - 433
How could I do the output in this case using left join method
Im lost
SELECT
c.id,
c.name,
GROUP_CONCAT(CONCAT(o.id,' - ',o.order))
FROM customer c
LEFT JOIN orders o
ON c.id = o.father_id
GROUP BY c.id
Related
Trying to get a simple SUM and COUNT from a table that takes a couple of joins and some math. I can't get it right.
I have 4 tables:
USERS
user_id, name
1, user1
2, user2
3, user3
4, user4
RESULTS
user_id, plus, minus
1, 1, 0
1, 1, 0
1, 1, 0
3, 0, 1
3, 1, 0
3, 1, 0
3, 1, 0
NOTES
user_id, note
1, lorem ipsum
3, abc
1, qwerty
3, qwerty
MESSAGES
user_id, message
1, lorem ipsum
3, abc
1, qwerty
3, qwerty
3, qwerty
3, qwerty
3, qwerty
I would like to sum all the result of subtracting plus from the minus, amount of notes and amount of messages of users.
My query:
SELECT u.`user_id`, SUM(w.`plus`-w.`minus`), COUNT(g.`user_id`), COUNT(m.`user_id`)
FROM `users` u
LEFT JOIN `results` w ON u.`user_id` = w.`user_id`
LEFT JOIN `notes` g ON u.`user_id` = g.`user_id`
LEFT JOIN `messages` m ON u.`user_id` = m.`user_id`
GROUP BY 1
Expected result should looks like:
user_id: sum(plus-minus) amount of notes, amount of messages
1: 3, 2, 2
3: 2, 2, 5
unfortunatelly query returns:
user_id: sum(plus-minus) amount of notes, amount of messages
1: 12, 12, 12
3: 20, 40, 40
Any help is greatly appreciated.
You should manually count the records in a subquery for every user_id so you will get correct value.
SELECT u.user_ID, u.Name,
COALESCE(r.totalResult, 0) totalResult,
COALESCE(n.totalNotes, 0) totalNotes,
COALESCE(m.totalMessages, 0) totalMessages
FROM users u
LEFT JOIN
(
SELECT user_ID, SUM(plus - minus) totalResult
FROM Results
GROUP BY user_ID
) r ON u.user_ID = r.user_ID
LEFT JOIN
(
SELECT user_ID, COUNT(*) totalNotes
FROM Notes
GROUP BY user_ID
) n ON u.user_ID = n.user_ID
LEFT JOIN
(
SELECT user_ID, COUNT(*) totalMessages
FROM Messages
GROUP BY user_ID
) m ON u.user_ID = m.user_ID
But if you have set an auto_incremented column in every table, the subquery isn't needed since you can distinctly count from it.
I have the folowing query that shows my products.
SELECT DISTINCT productsmap.id, attributes_product.waarde,
attributes_product.att_id, productsmap.name, productsmap.category
AS categoryId, brand.naam AS brand, productsmap.sku, categorie.name AS
category, productsmap.price, productsmap.shops
FROM productsmap
INNER JOIN categorie ON productsmap.category = categorie.id
INNER JOIN brand ON productsmap.brand = brand.id
INNER JOIN attributes_product ON productsmap.id = attributes_product.pmapid
WHERE productsmap.category
IN ( 2, 3, 4, 5, 6, 7, 8 )
AND productsmap.shops >0
AND (
productsmap.price >= 3.94
AND productsmap.price <= 204.99
)
AND productsmap.online =1
LIMIT 0 , 30
It gives the folowing results I cropped a bit:
(id) (waarde) (att_id) (name) (categoryId) (brand) (sku)
2 109 1 Name 4 Merk 70000
2 2013 2 Name etc etc etc
2 123 3 Name etc etc etc
3 103 1 Name2 etc etc
3 2012 2 Name2
3 123 3 Name2
4 110 1 3name
4 2013 2 3name
4 102 3 3name
Whit multiple times the same id and name only diffrent att_id and waarde. But i need to add to my query (attributes_product.att_id = 1 and waarde = 109) and (attributes_product.att_id = 2 and waarde = 2013)
But this is not possible because it are diffrent results. How can I solve this issue? Or on what can i search to solve this issue?
It attributes.product are att_id and waarde
I want to do a LEFT JOIN and then sort my display by a rank column and also show items only if the qty column is greater than 0
SELECT
*
FROM `product`
LEFT JOIN `stock`
ON `product`.`product_id`=`stock`.`pid`
AND `qty` > 1
ORDER BY `product`.`rank` ASC
Product Table
product_id
name
rank
price
Stock Table
pid
price_sale
qty
Product Table
12, Pen, 2, 53.00
13, Pen, 1, 58.00
14, Pen, 3, 25.00
Stock Table
12, 10.00, 5
13, 18.00, 15
My results do not appear.
It should display:
12, Pen, 2, 53.00, 12, 10.00, 5
13, Pen, 1, 58.00, 13, 18.00, 15
Use Below Query
SELECT * FROM `product`
LEFT JOIN `stock`
ON `product`.`product_id`=`stock`.`pid`
WHERE `stock`.`qty` > 0
ORDER BY `product`.`rank` ASC
The AND should be a WHERE like this :
SELECT *
FROM `product`
LEFT JOIN `stock` ON `product`.`product_id`=`stock`.`pid`
WHERE `qty` > 1
ORDER BY `product`.`rank` ASC
I have following table
id item_id rating
1 10 1
2 10 2
3 10 2
4 10 3
5 10 3
6 10 3
7 10 4
How can I write a query so that I can get result as follows:
$ratings = array(
1 => 1,
2 => 2,
3 => 3,
4 => 1,
5 => 0
);
I need to use this query to write a php function to calculate average ratings by this function:
$total = 0;
$count = 0;
foreach($ratings as $number=>$frequency) {
$total += $number * $frequency;
$count += $frequency;
}
return $total / $count;
The basic COUNTing of records on the table won't produce 5 because there is no value 5 on the rating. But generating subquery which contains values from 1-5 and joining them using LEFT JOIN will do your desired result.
SELECT a.rating, COUNT(b.rating) totalCount
FROM
(
SELECT 1 rating UNION SELECT 2 UNION
SELECT 3 UNION SELECT 4 UNION
SELECT 5
) a
LEFT JOIN tableName b
ON a.rating = b.rating
GROUP BY a.rating
SQLFiddle Demo
SELECT
rating,
COUNT(rating) as Count
FROM rating
GROUP BY rating
I am trying to manage seasonal prices for hotel rooms.
The only way that I can think of doing it would be to use:
A = Room Rate
B = Service Charge for room
Imagine that the table has a roomId column which is omited from below.
| DayDate |EndDate | A | B
-----------------------------------------------
| 2010/07/1 |2010/07/2 | 200 | 40
| 2010/07/3 |2010/07/4 | 150 | 40
| 2010/07/5 |2010/07/5 | 150 | 50
| 2010/07/6 |2010/07/7 | 200 | 50
| 2010/07/8 |2010/07/9 | 100 | 60
etc.. (table taken from another question).
The problem is: I don't want my seasons to be year specific.
Seasons for rooms shouldn't change year on year. I don't want my users to have to enter the seasonal information several times.
I am also going to have thousands of rooms, so I don't know a way to make this easily manageable.
I'm using mysql and php.
Start with a season table that defines the date ranges for the seasons. It should have a primary key field, say season_id. Then have another table to store room, price and season_id. The season_id is a foreign key to the season table.
Create Table Prices
(
MonthStart int not null
, DayStart int not null
, MonthEnd int not null
, DayEnd int not null
, A int not null
, B int not null
)
Insert Prices( MonthStart, DayStart, MonthEnd, DayEnd, A, B )
Select 7, 1, 7, 2, 200, 40
Union All Select 7, 3, 7, 4, 150, 40
Union All Select 7, 5, 7, 5, 150, 50
Union All Select 7, 6, 7, 7, 200, 50
Union All Select 7, 8, 7, 9, 100, 60
It should be noted that this approach presumes that the boundaries of the seasons are specific to the month and day regardless of year or circumstance. In addition, you'll have to decide how to handle leap year. Another approach which might be simpler is to simply enumerate every day of the year:
Create Table Prices
(
MonthStart int not null
, DayStart int not null
, A int not null
, B int not null
, Constraint PK_Prices Primary Key ( MonthStart, DayStart )
)
Insert Prices( MonthStart, DayStart, A, B )
Select 7, 1, 200, 40
Union All Select 7, 2, 200, 40
Union All Select 7, 3, 150, 40
Union All Select 7, 4, 150, 40
Union All Select 7, 5, 150, 50
Union All Select 7, 6, 200, 50
Union All Select 7, 7, 200, 50
Union All Select 7, 8, 100, 60
Union All Select 7, 9, 100, 60