groupBy and orderBy Laravel for getting higher value - php

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();

Related

finding products that are bought together Laravel eloquent

I am using Laravel and eloquent If I have the following two tables
Sales
----
|id|
|--|
|1 |
|2 |
|3 |
|4 |
----
Sales_data
----
|sale_id|Product_id|
|------------|
| 1 | 30 |
| 1 | 24 |
| 1 | 18 |
| 2 | 18 |
| 2 | 30 |
| 3 | 24 |
| 4 | 18 |
| 4 | 24 |
--------------
I would like to retrieve the following info:
| Product_id | bought_with | times_bought_together |
|--------------|-------------|-----------------------|
| 30 | 18 | 2 |
| 30 | 24 | 1 |
| 24 | 18 | 1 |
------------------------------------------------------
This is the relation in Sales model, to the sale_data
public function data()
{
return $this->hasMany(\App\Models\SaleData::class, 'sale_id');
}
Thank you verry much
You can use DB::select() so you can put RAW SQL statements in it.
In this case its easyer to just use RAW SQL
DB::select("SELECT c.product_id, c.bought_with, count(*) as times_bought_together
FROM (
SELECT a.product_id as product_id, b.product_id as bought_with
FROM sales_data a
INNER join sales_data b
ON a.sale_id = b.sale_id AND a.product_id != b.product_id) c
GROUP BY c.product_id, c.bought_with
ORDER BY times_bought_together DESC LIMIT 50")
GoodLuck if you have the same problem!

Multi table Mysql joint query in Laravel

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.

How to manage SELECT data from 3 different table's using with JOIN?

I have 3 different table's.
driver
| id | name |
|-----|------|
| 10 | abc |
| 21 | xyz |
booking
| id | driver_id | booking_amount |
|----|-----------|----------------|
| 1 | 10 | 250 |
| 2 | 10 | 150 |
| 3 | 21 | 200 |
| 4 | 21 | 300 |
income
| id | driver_id | credit | debit | date |
|----|-----------|--------|-------|----------|
| 1 | 10 | 100 | | 1-1-2019 |
| 2 | 10 | 250 | | 2-1-2019 |
| 3 | 10 | | 200 | 3-1-2019 |
| 4 | 21 | 250 | | 1-1-2019 |
| 5 | 21 | 400 | | 2-1-2019 |
| 6 | 21 | | 50 | 3-1-2019 |
driver.id = booking.driver_id
driver.id = income.driver_id
I have use this query >>
SELECT driver.*, sum(booking.total_booking_income) as total_income
FROM driver
JOIN booking ON driver.id=booking.driver_id
GROUP BY driver.id
ORDER BY driver.id DESC
Note : but i am not able to add balance field in my this query.
i want to all driver records of income after group of booking and group of income by driver id like
| id | driver_id | driver_name | total_income | balance |
|----|-----------|-------------|--------------|---------|
| 1 | 10 | abc | 400 | -250 |
| 2 | 21 | xyz | 500 | 100 |
Assuming balance is the difference between the credit and the debit then:
SELECT d.*, sum(b.total_booking_income) as total_income,
i.balance
FROM driver d JOIN
booking b
ON d.id = b.driver_id LEFT JOIN
(SELECT i.driver_id,
SUM(i.credit) - SUM(i.debit) as balance
FROM income i
GROUP BY i.driver_id
) i
ON i.driver_id = d.id
GROUP BY d.id, i.balance
ORDER BY d.id DESC;

Mysql select distinct id's and select first row only

This is my table. I want to select distinct 'bill_no' with only first related row.
+--------------+--------------+-------+------------+
| id | bill_no | product_name | tax | total |
+--------------+-------+------+-------+------------+
| 1 | 12 | Hairgel | 10 | 241 |
| 2 | 12 | Spiker gel | 10 | 300 |
| 3 | 13 | Wokzem amba | 12 | 450 |
| 4 | 13 | test prod | 1 | 145 |
| 5 | 14 | wokk | 3 | 55 |
| 6 | 14 | Kamer hyp | 11 | 46 |
| 7 | 15 | Xyombokx | 2 | 220 |
+--------------+-------+------+-------+------------+
I want data to be displayed like the below table having only distinct "bill_no" -
Output-
+--------------+--------------+-------+------------+
| id | bill_no | product_name | tax | total |
+--------------+-------+------+-------+------------+
| 1 | 12 | Hairgel | 10 | 241 |
| 3 | 13 | Wokzem amba | 12 | 450 |
| 5 | 14 | wokk | 3 | 55 |
| 7 | 15 | Xyombokx | 2 | 220 |
+--------------+-------+------+-------+------------+
Use group by
select * from youtable group by bill_no
select t1.*
from your_table t1
join
(
select min(id) as id
from your_table
group by bill_no
) t2 on t1.id = t2.id
You can use GROUP BY clause.GROUP BY clause always return first row with related group by cloumn name.
SELECT * FROM `table_1` group by `bill_no`

fetch data from multiple tables

I have 4 table for my Ads Portal.
Firstly main table name is ads
+-------------+
| Field +
+-------------+
| id +
| ads_title +
+-------------+
example data for ads table;
+----+-----------------+
| id | ads_title |
+----+-----------------+
| 20 | 2006 CITROEN C4 |
| 27 | EMEKLI OGRETMEN |
| 28 | Harika 10 n |
| 34 | HATASIZ BOYASIZ |
| 49 | BAYANDAN 2009 |
+----+-----------------+
for ads specifications stored in a table. table name is ads_spc
+---------+
| Field |
+---------+
| id |
| key_name|
+---------+
data example for ads_spc table;
+----+-------------+
| id | key_name |
+----+-------------+
| 1 | Date |
| 2 | Km |
| 3 | Colr |
| 4 | Engine |
| 5 | Pw. Engine |
| 6 | Oil |
| 7 | Speed |
| 8 | Boody Type |
| 9 | Traction |
| 10 | Warranty |
+----+-------------+
Lastly stored specification values for ads_spc by ads_id. table name is ads_values
+------------+
| Field |
+------------+
| id |
| ads_id |
| spc_id |
| value |
+------------+
for example this table data
+----+--------+--------+-------+
| id | ads_id | spc_id | value |
+----+--------+--------+-------+
| 25 | 49 | 9 | 2119 |
| 26 | 49 | 10 | 2131 |
| 27 | 34 | 1 | 2010 |
| 28 | 34 | 2 | 8900 |
| 29 | 34 | 3 | 4 |
| 30 | 34 | 4 | 22 |
| 31 | 34 | 5 | 40 |
| 32 | 34 | 6 | 60 |
| 33 | 34 | 7 | 65 |
| 34 | 34 | 8 | 66 |
+----+--------+--------+-------+
I want to using these tables list ads (table: ads) with specifications (table:ads_spc) and ads values (table:ads_values) in a data row.
Union to;
ADS + ADS_SPC + ADS_VALUES (All values searchable)
I'm using this case:
SELECT SQL_CALC_FOUND_ROWS
*
FROM ads AS a
LEFT JOIN ads_spc AS aspc
LEFT JOIN ads_values AS aval ON aval.ads_id=a.id AND aval.spc_id=aspc.spc_id
How can I union to one row?
Sorry for my english.
Why union there is no sense for the union you should join these tables
SELECT v.id, a.ads_title,s.key_name,v.value FROM `ads` a
INNER JOIN `ads_values` v ON (a.id =v.ads_id)
INNER JOIN `ads_spc` s ON(v.spc_id =s.id) WHERE s.key_name='Color'

Categories