Here two tables.
table1:Customer
+----+-------+-------------+--------+
| id | Ip_Id | Customer_Id | Class |
+----+-------+-------------+--------+
| 1 | 100 | A | First |
| 2 | 100 | B | First |
| 3 | 100 | C | First |
| 4 | 101 | D | First |
| 5 | 101 | E | First |
| 6 | 100 | F | Second |
+----+-------+-------------+--------+
table2:Customer_Usage
+-------+-------------+----------------+--------+------------+
| Ip_Id | Customer_Id | Customr _Usage | Amount | Date |
+-------+-------------+----------------+--------+------------+
| 100 | A | 10 | 100 | 21-02-2020 |
| 100 | B | 20 | 200 | 22-02-2020 |
| 100 | A | 30 | 500 | 23-03-2020 |
| 100 | B | 40 | 500 | 24-02-2020 |
+-------+-------------+----------------+--------+------------+
The condition is while enter a value for search Ip_Id, example for 100 it will give a result like below. make average of the values from the date. How to make a joint and calculate average two columns Avg _Usage,Avg_Amount for these two tables in Laravel using Mysql
Result:
+-------+-------------+------------+------------+
| Ip_Id | Customer_Id | Avg _Usage | Avg_Amount |
+-------+-------------+------------+------------+
| 100 | A | 20 | 300 |
| 100 | B | 30 | 350 |
+-------+-------------+------------+------------+
Am using below query.
$results = DB::table('Customer')
->select('Customer.Ip_Id','Customer.Customer_Id', 'Customer_Usage.Usage')
->where('Ip_Id', 100)
->get();
Use method-join to join Customer table and Customer_Usage table,
And groupBy CustomerId for calculating the average of Customr_Usage and Amount in each group.
$results = DB::table('Customer')
->select('Customer.Ip_Id','Customer.Customer_Id', DB::raw('AVG(Customer_Usage.Customr_Usage) AS Avg_Usage'), DB::raw('AVG(Customer_Usage.Amount) AS Avg_Amount'))
->join('Customer_Usage', 'Customer_Usage.customer_id', '=', 'Customer.customer_id')
->where('Customer.Ip_Id', 100)
->groupBy('Customer.Customer_Id')
->get();
You can use AVG, groupBy and raw query for the same;
DB::table('Customer')
->selectRaw('Customer.Ip_Id','Customer.Customer_Id', 'AVG(Customer_Usage) as Avg_Usage', 'AVG(Amount) as Avg_Amount')
->join('Customer_Usage', 'Customer_Usage.customer_id', '=', 'Customer.customer_id')
->where('Ip_Id', 100)
->groupBy("Customer_Id")
->get();
Laravel -> Queries -> raw method
Related
Am trying to connect two tables Customer and Customer_Usage for getting the result.Tables are showing below.
Table: Customer
+----+-------+-------------+--------+
| id | Ip_Id | Customer_Id | Class |
+----+-------+-------------+--------+
| 1 | 100 | A | First |
| 2 | 100 | B | First |
| 3 | 100 | C | First |
| 4 | 101 | D | First |
| 5 | 101 | E | First |
| 6 | 100 | F | Second |
+----+-------+-------------+--------+
Table: Customer_Usage
+----+-------------+----------------+
| id | Customer_Id | Usage |
+----+-------------+----------------+
| 1 | A | 1245 |
| 2 | B | 4414 |
| 3 | C | 8521 |
| 4 | D | 2314 |
| 5 | E | 521 |
| 6 | F | 5412 |
+----+-------------+----------------+
The condition is while enter a value for search Ip_Id, example for 100 it will give a result like below. How to make a joint for these two tables in Laravel using Mysql
Output result
+-------+-------------+----------------+
| Ip_Id | Customer_Id | Usage |
+-------+-------------+----------------+
| 100 | A | 1245 |
| 100 | B | 4414 |
| 100 | C | 8521 |
| 100 | F | 5412 |
+-------+-------------+----------------+
This is the query am trying.
$result = DB::table('Customer')
->where('Ip_Id','=','100')
->get();
Left join is recommended to achieve what you need.
DB::table('Customer')
->select('Customer.Ip_Id','Customer.Customer_Id','Customer_Usage.Usage')
->leftJoin('Customer_Usage', 'Customer_Usage.Customer_Id', '=', 'Customer.Customer_Id')
->where('Customer.Ip_Id',100)
->get();
Use Eloquent Inner Join like:
$results = DB::table('Customer')
->select('Customer.Ip_Id','Customer.Customer_Id', 'Customer_Usage.Usage')
->join('Customer_Usage','Customer.Customer_Id', '=','Customer_Usage.Customer_Id')
->where('Ip_Id', 100)
->get();
You will get the desired output like above.
this is my database tables
Lists
Requests
products
Every request has one product and every list has many requests
now i want to get the min and max price/size for the products in list X , how can i achieve this with Laravel Elequent ?
Lists table
+----+----------+
| id | user_id |
+====+==========+
| 1 | 1 |
+----+----------+
| 2 | 1 |
+----+----------+
| 3 | 2 |
+----+----------+
Requests table
+----+-------------+----------+
| id | product_id | list_id |
+====+=============+==========+
| 1 | 1 | 1 |
+----+-------------+----------+
| 2 | 1 | 1 |
+----+-------------+----------+
| 3 | 2 | 2 |
+----+-------------+----------+
Products table
+----+-------+------+-------+
| id | price | size | other |
+====+=======+======+=======+
| 1 | 110 | 10 | test |
+----+-------+------+-------+
| 2 | 130 | 12 | test |
+----+-------+------+-------+
| 3 | 100 | 24 | test |
+----+-------+------+-------+
| 4 | 200 | 16 | test |
+----+-------+------+-------+
I want to get the min and max price/size for the products in list X
$maxSize = Product::whereHas('request.list', function($q) use($listId) {
$q->where('list.id', $listId);
})
->max('size');
$minSize = Product::whereHas('request.list', function($q) use($listId) {
$q->where('list.id', $listId);
})
->min('size');
Do the same for the min and max price.
I have problem when grouping and ordering the result of my query.
here is my data
+--------+-----------+----------+--------+-------+
| id_krs | id_mhs_pt | id_kelas | status | nilai |
+--------+-----------+----------+--------+-------+
| 38 | 3 | 29 | B | A |
| 45 | 3 | 30 | B | A |
| 46 | 3 | 31 | B | C |
| 18393 | 3 | 31 | U | A |
+--------+-----------+----------+--------+-------+
here is my query
DB::table('krs')->select('id_krs','id_mhs_pt','id_kelas','status','nilai')
->where('id_mhs_pt',3)->groupBy('id_kelas')->orderBy('nilai','asc')->get();
when i run the query, the result is:
+--------+-----------+----------+--------+-------+
| id_krs | id_mhs_pt | id_kelas | status | nilai |
+--------+-----------+----------+--------+-------+
| 38 | 3 | 29 | B | A |
| 45 | 3 | 30 | B | A |
| 46 | 3 | 31 | B | C |
+--------+-----------+----------+--------+-------+
and the result i want is
+--------+-----------+----------+--------+-------+
| id_krs | id_mhs_pt | id_kelas | status | nilai |
+--------+-----------+----------+--------+-------+
| 38 | 3 | 29 | B | A |
| 45 | 3 | 30 | B | A |
| 18393 | 3 | 31 | U | A |
+--------+-----------+----------+--------+-------+
Because the id_krs 18393 have greater nilai (score) than id_krs 46 (both using same id_kelas). I tried to change the order to asc or desc, but the result still the same. How to get that? Thank's!
The problem is that you loose the order on group by, so you must use a temporary table to get the minimum nilai for each id_kelas then join it in order to fetch the other columns:
DB::table('krs')
->select('id_krs', 'id_mhs_pt', 'id_kelas', 'status', 'nilai')
->leftJoin(
DB::raw('(select id_krs, min(nilai) as m_nilai FROM krs GROUP BY id_kelas) tmp'), function ($join) {
$join
->on(DB::raw('tmp.id_krs'), '=', DB::raw('krs.id_krs'))
->on(DB::raw('tmp.m_nilai'), '=', DB::raw('krs.nilai'));
}
)
->where('id_mhs_pt', 3)
->orderBy('nilai', 'asc')
->get();
I have following table name is sk_event:
|------------------------------------------------------------|
| S.No | ticket_id | event_name | sell_amount | coupon_code |
|------------------------------------------------------------|
| 1 | 5 | Airtel | 450 | ABC |
| 2 | 5 | Airtel | 500 | No Code |
| 3 | 6 | Airtel | 250 | XYZ |
| 4 | 5 | Airtel | 450 | ABC |
| 5 | 6 | Airtel | 250 | XYZ |
| 6 | 5 | Airtel | 450 | ABC |
|------------------------------------------------------------|
Second table i have which name is sk_ticket
|------------------------------------|
| S.No. | Ticket_name | ticket Price |
| 5 | 10 KM | 500 |
| 6 | 5 KM | 300 |
|------------------------------------|
I am using following SQL query :
mysqli_query($con, "SELECT * FROM sk_event e INNER JOIN sk_ticket t ON e.ticket_name = t.ticket_name GROUP BY t.ticket_name") or die(mysqli_error($con));
This query is not giving me the result which I want. I want following answer which will be show in datatable bootstraps. I know how to use datatable bootstraps. I just want child row data.
|-------------------------------------------------------------------|
| # | Ticket Name | base Price | Sell Price | Coupon Used | Total |
|-------------------------------------------------------------------|
| + | 10 KM | 500 | - | - | 4 |
| | 10 Km | - | 450 | ABC | 3 |
| | 10 Km | - | 500 | No Code | 1 |
|-------------------------------------------------------------------|
| + | 5 Km | 300 | - | - | 2 |
| | 5 Km | - | 250 | XYZ | 2 |
|-------------------------------------------------------------------|
So, in the above there are two rows result which is indicate by + and every row have some child rows which bifurcate coupon wise data.
How can I show my data like the above table?
Your tried query will not works because you have tried wrong join query, you should do inner join with primary key and foreign key instead of your tried way. Because joins can perform only with primary key and foreign keys.
You can try below query. Hope below query will work for you and you can get your required data.
select * from sk_event se inner join sk_ticket st on se.ticket_id = st.S.No.
I have a database table campaign_data. I need to select the customer_id where in the campaign there is difference in tariff. How can i do that with MySQL query. Here is some sample data.
SQL Fiddle Schema
| CAMPAIGN_ID | CUSTOMER_ID | CAMPAIGN_NAME | TARIFF |
---------------------------------------------------------
| 1 | 1 | Richmond | 100 |
| 2 | 1 | Sutton Coldfield | 75 |
| 3 | 1 | Putney | 100 |
| 4 | 1 | Kentish Town | 100 |
| 5 | 1 | Woking | 100 |
| 6 | 2 | Chiswick | 90 |
| 7 | 2 | Ealing | 100 |
| 8 | 2 | Camden | 100 |
| 9 | 3 | Croydon | 75 |
| 10 | 3 | Croydon1 | 100 |
| 11 | 3 | Archway | 100 |
| 12 | 4 | Ealing0 | 100 |
| 13 | 4 | Ealing01 | 100 |
| 14 | 4 | Ealing02 | 100 |
| 15 | 4 | Chingford | 100 |
| 16 | 4 | chingford01 | 100 |
Now as you can see customer id 1 , and 3 has different tariffs. I want to select them and leave the customer id 4 because it has campaigns with same tariffs.
Desired Output
| CUSTOMER_ID |
---------------
| 1 |
| 2 |
| 3 |
For clearification you can see customer 1 has 5 records. If in his 5 records the tariff is same (100) i want to avoid but if the tariff is not some as 4 records have 100 and one has 75, i want to select.
SELECT customer_id, count(DISTINCT tariff) as tariffs
FROM campaign_data
GROUP BY customer_id
HAVING tariffs > 1
you looking for this maybe
SELECT customer_id
FROM campaign_data
GROUP BY customer_id
HAVING count(DISTINCT tariff) > 1
http://sqlfiddle.com/#!2/48b6e/31
select
customer_id,
tariff
from campaign_data
group by customer_id
having sum(tariff)/count(tariff) <> tariff;