Retrive data from 2 tables with multiple conditions - php

I have a question, So I have 2 tables :
order:
id supplier_id status
1 2 3
2 4 5
3 2 7
gift_2_order:
order_id gift_id order_price
1 2 4
2 1 5
3 2 6
1 6 1
2 4 9
So I want to get all orders, how many times is repeated in table : gift_2_order and the total price. For this example I want to get :
order number_repetitions total_price
1 2 5
2 2 14
3 1 6
I tried :
SELECT
ord.estimation_date,
ord.order_date,
ord.supplier_id,
ord.status,
count.(g2o.order_id),
sum.(g2o.order_price)
FROM `order` ord
INNER JOIN gift_2_order g2o on ord.id = g2o.order_id
Can you help me please? Thank's in advance.

you have to group by ord.id and remove . from count and sum
SELECT
ord.id,
count(g2o.order_id),
sum(g2o.order_price)
FROM `order` ord
INNER JOIN gift_2_order g2o on ord.id = g2o.order_id
group by ord.id order by ord.id

Try:
SELECT
ord.order_id,
count(g2o.order_id) as number_repetitions ,
sum(g2o.order_price) as total_price
FROM `order` ord
INNER JOIN gift_2_order g2o on ord.id = g2o.order_id
group by ord.order_id
By the way "order" is no very good table name.

SELECT
t1.supplier_id,
t1.status,
count(t2.order_id) as total_orders,
sum(t2.order_price) as total_amount
FROM `order` t1
join gift_2_order t2 where t1.id = t2.order_id group by t2.order_id

You don't have the need of doing a JOIN. Without a JOIN the query will be more performant...
This SQL fiddle will do the thrick for you:
SELECT
order_id,
count(order_id),
sum(order_price)
FROM gift_2_order
GROUP BY order_id
ORDER BY order_id
If you do not need to join tables, don't do it.

Related

How to get avg rating per product using codeigniter query?

I have rating table which have rating for every product given by user, I am retrieving all rating records, but at the same time I want to get avg rating based on per product but I am unable to get ouptput
Query :
$this->db->select('ratings.*');
$this->db->select('select AVG(ratings) as avg_rating from ratings group by product_id');
$this->db->from('ratings');
$this->db->join('users','users.id=ratings.user_id');
$this->db->get()->result();
Rating table
id user_id product_id rating
1 4 1 4
2 5 2 4
3 6 1 2
4 7 4 4
Expected output:
id user_id product_id rating avg rating
1 4 1 4 3
2 5 2 4 4
3 6 1 2 3
4 7 4 4 4
get data from table ratings, using a left join with select for the average.
the join() function of Codeigniter allows you to to write a select part instead of the table name, but you need to place it into parenthesis:
$this->db->select('t1.*, t2.avg_rating, t3.*');
$this->db->from('ratings t1');
$this->db->join('
(select product_id, avg(rating) as avg_rating
from ratings
group by product_id) t2','t2.product_id=t1.product_id','left'
);
$this->db->join('users t3','t3.id=t1.user_id','left');
$this->group_by('t1.userid')
$this->db->get()->result();
generates:
SELECT t1.*, t2.avg_rating, t3.*
FROM ratings t1
left join
(select product_id, avg(rating) as avg_rating from ratings group by product_id) t2
on t2.product_id=t1.product_id
left join users t3
on t1.user_id = t3.id
group by t1.user_id
and outputs as you expect.
When Queries get complicated i like to use query instead of query builder. You could do this:
$this->db->query('select r.*,(select round(sum(r2.rating)/count(*),0) from ratings r2 where r2.product_id = r.product_id ) as 'avg rating' from ratings r')->result();

Count in Joins in SQL

i have two tables (post.id is primary key which become seconday key in like.post_id )
table "post"
id user_id image
1 10 abc.jpg
2 20 xyz.jpg
3 10 ajb.jpg
Table "Like"
id user_id post_id likes
1 10 1 1
2 20 2 1
3 10 1 1
4 10 1 1
3 10 3 1
now i want whenever i pass user_id then i wan to get all post of users with number of likes of posts
i tried with following code but not worked,
SELECT selfie_info.id,selfie_info.user_id,selfie_info.image, (SELECT COUNT(m.likes)FROM post_likes m WHERE m.user_id='10') as total_likes FROM selfie_info where user_id='10'
how can i do this ? i want result like following (if i pass user_id=10 )
user_id post_id likes
10 1 3
10 3 1
SELECT p.user_id, p.id AS post_id, COUNT(l.id) AS total_likes
FROM post p
LEFT JOIN likes l ON l.post_id =p.id
WHERE p.user_id=10 GROUP BY p.id;
You can use aggregation:
select pl.post_id, count(*) as numlikes,
si.*
from selfie_info si join
post_likes pl
on si.user_id = pl.user_id
where si.user_id = 10
group by si.user_id, si.post_id
You can use si.* in the select, even though this is a group by query because you are aggregating by the primary key/unique id in the table.
You have all necessary data in table "like"
You can use one select from "like" table.
select user_id, post_id, sum(likes) as likes from like where user_id = 10;
You need to count or sum likes?
Depends on you needs You can sum(likes) or count(*) likes

php - mysql join three tables and grouping

I have three tables:
products:
id name
1 juice
2 chips
3 water
orders:
id product_id order_id
1 1 special1
2 3 special1
3 2 special1
4 1 special2
5 2 special2
final_orders:
id order_id date
1 special1 25-3-2017
2 special2 25-3-2017
I want to select all products names in every order using order_id to show:
ID: Special1
Date: 25-3-2017
Products List:
juice
water
chips
ID: Special2
Date: 25-3-2017
Products List:
juice
chips
I use this:
$sql = "select * from products,orders where products.id = orders.product_id";
but it doesn't work and show me duplicated results.
thank you.
You need to join with final_orders as well:
SELECT *
FROM final_orders AS f
JOIN orders AS o ON f.order_id = o.order_id
JOIN products AS p ON p.id = o.product_id
ORDER BY f.order_id
To prevent duplication in the output, your loop that prints the output should only show the information from final_orders when it changes. See How can i list has same id data with while loop in PHP?
If you want to see one final order per record in your result set, then you will have to aggregate the products which appear in each order. One option then is the following query which aggregates order products into CSV using MySQL's GROUP_CONCAT():
SELECT t1.order_id,
t1.date,
t2.products
FROM final_orders t1
INNER JOIN
(
SELECT a.order_id, GROUP_CONCAT(b.name) AS products
FROM orders a
INNER JOIN products b
ON a.product_id = b.id
GROUP BY a.order_id
) t2
ON t1.order_id = t2.order_id
Demo here:
Rextester

Sql request into 2 tables to get percentage of each rows

I have 2 SQL tables like this:
___Rooms:
ROO_Id ROO_Number
1 101
2 201
___Bookings:
BOO_Id BOO_RoomNumber
1 1
2 1
3 2
I want to echo a table with all rooms I have with percentage for each.
BOO_RoomNumber percentage count
101 66% 2
201 33% 1
Thanks.
Here is the answer:
UPDATED
select r.ROO_Number BOO_RoomNumber, ((ifnull(cnt_book,0)*100)/(select count(*) from Bookings)) percentage, ifnull(cnt_book,0) `count`
from Rooms r left join (select BOO_RoomNumber, count(*) cnt_book
from Bookings
group by BOO_RoomNumber) cnt on r.ROO_Id=cnt.BOO_RoomNumber
Working SQLFiddle
Try to use this:
SELECT r.id as BOO_RoomNumber, ((booked/(select count(id) from BOOKINGS))*100) as percentage, booked as `count`
FROM ROOMS as r
LEFT JOIN
(
SELECT count(id) as booked, roomId
FROM BOOKINGS
GROUP BY roomId
) as b on b.roomId=r.id

adding and multiplying COUNT() of several tables

Is it possible to add and multiply the count of different tables where the id is the same?
Imagine:
Table_1 Table_2 Table_3
id id id
1 1 1
1 2 2
2 2 3
3 2 3
3 2 3
3 3 3
So that the end result would be this table with 2 columns:
id (COUNT(Table_1.id) + 2*COUNT(Table_2.id) + 3*COUNT(Table_3.id))
1 7
2 12
3 17
I don't know if I understood you correctly but give this a try,
SELECT a.ID,
a.aa + (2 * b.bb) + (3 * c.cc)
FROM
(
SELECT ID, COUNT(*) aa
FROM table1
GROUP BY ID
) a LEFT JOIN
(
SELECT ID, COUNT(*) bb
FROM table2
GROUP BY ID
) b ON a.ID = b.ID
LEFT JOIN
(
SELECT ID, COUNT(*) cc
FROM table3
GROUP BY ID
) c ON a.ID = c.ID
SQLFiddle Demo
SELECT id, counts_1.number + 2 * counts_2.number + 3 * counts_3.number
FROM
(SELECT id, COUNT(*) AS number FROM Table_1 GROUP BY id) AS counts_1
JOIN
(SELECT id, COUNT(*) AS number FROM Table_2 GROUP BY id) AS counts_2 USING (id)
JOIN
(SELECT id, COUNT(*) AS number FROM Table_3 GROUP BY id) AS counts_3 USING (id)
Note that this solution requires that every id exists at least once in each of the tables, otherwise it will be left out of the result. Changing this would require a FULL OUTER JOIN that MySQL is incapable of. There are ways around that limitation, though.

Categories