Multi table Mysql joint query in Laravel - php

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.

Related

Mysql Query Or Laravel Eloquent for Average of multiple column

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

How to join other table and count the row in laravel?

I am trying to count how many require position there are for each jobseeker.
I have two tables: jobseeker and jobposition.
jobseeker:
+----+---------+-----------+----------------+
| id | fb_name | fullname | desireposition |
+----+---------+-----------+----------------+
| 1 | John | John Cena | 3 |
| 2 | Christ | Christ | 4 |
| 3 | Thomas | Cfitcher | 2 |
+----+---------+-----------+----------------+
and jobposition:
+----+--------+------------------+
| id | job_id | require_position |
+----+--------+------------------+
| 1 | 12 | 3 |
| 2 | 13 | 3 |
| 3 | 14 | 4 |
| 4 | 15 | 5 |
| 5 | 16 | 4 |
| 6 | 17 | 3 |
+----+--------+------------------+
My expected result is:
+----+---------+-----------+----------------+-----------------------+
| id | fb_name | fullname | desireposition | total_requireposition |
+----+---------+-----------+----------------+-----------------------+
| 1 | John | John Cena | 3 | 3 |
| 2 | Christ | Christ | 4 | 2 |
| 3 | Thomas | Cfitcher | 2 | 0 |
+----+---------+-----------+----------------+-----------------------+
I want to count how many require position there for each jobseeker.
Here is what I tried using crossJoin, but am unsure which join I actually need to be using.
$jobseekers = Jobseeker::crossJoin('jobpositions')
>select('fullname','fb_name','desire_position', DB::raw('count(require_position) as total_requireposition'))
->groupBy('fullname')->paginate(10);
Can anyone help guide me? Any help would be highly appreciated.
The regular MySQL query you want is:
SELECT s.id, fullname, fb_name, desireposition, IFNULL(COUNT(require_position), 0) AS require_position
FROM jobseeker AS s
LEFT JOIN jobposition AS p ON s.desireposition = p.require_position
GROUP BY s.id
I don't use Laravel, but I think the translation would be:
$Jobseeker->select('fullname','fb_name','desire_position', DB::raw('IFNULL(COUNT(require_position), 0) as total_requireposition'))
->leftjoin('jobposition', 'desireposition', '=', 'require_position')
->groupBy('jobseeker.id')
->paginate(10)

Cross database multiple records with join in MySQL

In this case each person can be assigned to an unlimited number of companies. One of these assigned companies should always be the person's main company. As you can see in the SQL query which I have posted the ID of the person's main organization is stored in the column "main_company_id" in the "person" table.
I have database Internal on server 192.168.0.1 with Person_Company Table as given
+----+-----------+------------+------------+
| id | person_id | company_id | created_at |
+----+-----------+------------+------------+
| 1 | 1005 | 2589 | 00:00:00 |
| 2 | 1006 | 2590 | 00:00:00 |
| 3 | 1007 | 2591 | 00:00:00 |
+----+-----------+------------+------------+
Person Table is as given
+------+-------+---------+-----------------+-----------------+
| id | name | phone | main_company_id | ref_id |
+------+-------+---------+-----------------+-----------------+
| 1005 | John | 0123456 | 2590 | 273722702297768 |
| 1006 | Doe | 7894560 | 2591 | 955413080598021 |
| 1007 | Smith | 9517530 | 2589 | 164283934074454 |
+------+-------+---------+-----------------+-----------------+
Company Table is as given
+------+-----------+---------+-----------------+
| id | name | vat | ref_id |
+------+-----------+---------+-----------------+
| 2589 | Company A | 0123456 | 540603005841231 |
| 2590 | Company B | 7894560 | 725472422399397 |
| 2591 | Company C | 9517530 | 367043795528136 |
+------+-----------+---------+-----------------+
Now there is another database External on the same server 192.168.0.1 with External_Person tables as given:
+----+-----------------+-------+----------------------+--------+
| id | ref_id | name | internal_primary_key | gender |
+----+-----------------+-------+----------------------+--------+
| 1 | 273722702297768 | John | ABC123456 | male |
| 2 | 955413080598021 | Doe | BCD456789 | female |
| 3 | 164283934074454 | Smith | DEF789456 | male |
+----+-----------------+-------+----------------------+--------+
And another table on this External database is External_Company
+----+-----------------+-----------+----------------------+
| id | ref_id | name | internal_primary_key |
+----+-----------------+-----------+----------------------+
| 1 | 540603005841231 | Company A | XX4123456 |
| 2 | 725472422399397 | Company B | XX5456789 |
| 3 | 367043795528136 | Company C | XX6789456 |
+----+-----------------+-----------+----------------------+
What I want to achieve is like this result:
+----+------------+-------------+------------------+
| id | person_key | company_key | main_company_key |
+----+------------+-------------+------------------+
| 1 | ABC123456 | XX4123456 | XX5456789 |
| 2 | BCD456789 | XX5456789 | XX6789456 |
| 3 | DEF789456 | XX6789456 | XX4123456 |
+----+------------+-------------+------------------+
I have already achieved two columns through this statement:
SELECT EEP.internal_primary_key as Person_Key, EEC.internal_primary_key as Company_Key
FROM Internal.Person_Company as IPC
JOIN Internal.Person as IP on IP.id = IPC.person_id
JOIN Internal.Company as IC on IC.id = IPC.company_id
JOIN External.External_Person as EEP on IP.ref_id = EEP.ref_id
JOIN External.External_Company as EEC on IC.ref_id = EEC.ref_id
WHERE IPC.person_id = 1005;
And this has the output like this result:
+----+------------+-------------+
| id | person_key | company_key |
+----+------------+-------------+
| 1 | ABC123456 | XX4123456 |
| 2 | BCD456789 | XX5456789 |
| 3 | DEF789456 | XX6789456 |
+----+------------+-------------+
How can I get the main company's internal_primary_key of the person in this given scenario? How can I amend my this existing query to achieve the desired result which I have mentioned above?
Does this work for you? You have a lot of good work, it seems all you would need is add the EEC.internal_primary_key as main_company_key code to your select statement.
SELECT EEP.internal_primary_key as person_Key, EEC.internal_primary_key as company_Key, EEC.internal_primary_key as main_company_key
FROM Internal.Person_Company as IPC
JOIN Internal.Person as IP on IP.id = IPC.person_id
JOIN Internal.Company as IC on IC.id = IPC.company_id
JOIN External.External_Person as EEP on IP.ref_id = EEP.ref_id
JOIN External.External_Company as EEC on IC.ref_id = EEC.ref_id
WHERE IPC.person_id = 1005;
I have done this successfully after doing some research upon databases
SELECT EEP.internal_primary_key as Person_Key, EEC.internal_primary_key as Company_Key, MEEC.internal_primary_key as Main_Company_Key
FROM Internal.Person_Company as IPC
JOIN Internal.Person as IP on IP.id = IPC.person_id
JOIN Internal.Company as IC on IC.id = IPC.company_id
JOIN External.External_Person as EEP on IP.ref_id = EEP.ref_id
JOIN External.External_Company as EEC on IC.ref_id = EEC.ref_id
JOIN Internal.Company as MIC on MIC.id = IP.main_company_id
JOIN External.External_Company as MEEC on MIC.ref_id = MEEC.ref_id
WHERE IPC.person_id = 1005;
And now this results as I wanted:
+----+------------+-------------+------------------+
| id | person_key | company_key | main_company_key |
+----+------------+-------------+------------------+
| 1 | ABC123456 | XX4123456 | XX5456789 |
| 2 | BCD456789 | XX5456789 | XX6789456 |
| 3 | DEF789456 | XX6789456 | XX4123456 |
+----+------------+-------------+------------------+

How to get Lowest value of each group in mysql

I want to get lowest value of each group from two table
Table Are Below
Table 1 Table 2
| GPN | Amt | | GPN | Date |
| A | 10 | | A | 2016-09-10 |
| A | 15 | | A | 2016-09-18 |
| A | 20 | | B | 2016-09-10 |
| A | 25 | | B | 2016-09-11 |
| A | 30 | | B | 2016-09-12 |
| B | 20 | | C | 2016-10-12 |
| B | 40 | | C | 2016-10-13 |
| B | 60 | | C | 2016-10-14 |
| B | 80 | | D | 2016-09-10 |
| B | 100 | | D | 2016-10-13 |
| C | 3 |
| C | 6 |
| C | 9 |
| C | 12 |
| C | 15 |
| D | 7 |
| D | 10 |
| D | 13 |
| D | 16 |
| D | 19 |
| D | 22 |
How i want that value
For Example
Date = 2016-09-10,
On That how many GPN are there so i have to get every GPN's Lowest Amt
So Result Will be like this
Result
| GPN | Amt |
| A | 10 |
| B | 20 |
| D | 7 |
I have tried by using ASC LIMIT 1 so obviously it will show only one raw but but i have no idea how to do that.
and i did it with php loop but i am looking if it is possible in mysql query so that will awesome.
Inner join and group by
select table1.GPN, min(table2.Amt)
from table1
inner join table2 on table1.GPN= table2.GPN
where date(table2.date ) = str_to_date('2016-09-10', '%Y-%m-%d')
group by table1.GPN
This is a classic aggregate function query.
SELECT T1.GPN, MIN(Amt) FROM Table1 T1 INNER JOIN Table2 T2 ON T1.GPN = T2.GPN GROUP BY T1.GPN WHERE T2.Date = ?

mysql inner join 2 tables and order by count

I am having the following tables in my DB
PROJECTS
+----+-------------------------------------------+
| id | name |
+----+-------------------------------------------+
| 1 | YANNONALI COURT |
| 2 | UNIVERSITY OF COLORARDO DENVER RESEARCH 2 |
| 3 | G.R.E.A.T PROGRAM DESALTER BUILDING |
| 4 | MONARCH CLUB |
| 5 | LAFAYETTE MERCANTILE |
| 6 | CAMELBACK VILLAGE RAQUET AND HEALTH CLUB |
| 7 | BACK COUNTRY |
| 8 | URBAN CRASHPAD |
| 9 | PRIVATE RESIDENCE |
| 10 | EATON RESIDENCE |
+----+-------------------------------------------+
PROJECT_ASSIGNMENTS(WHERE projects.id=project_assignment.target_id)
+-------+-----------+-------------+
| id | target_id | property_id |
+-------+-----------+-------------+
| 19178 | 1 | 48 |
| 19192 | 1 | 39 |
| 19391 | 1 | 3 |
| 19412 | 2 | 3 |
| 19591 | 2 | 34 |
| 19610 | 2 | 34 |
| 21013 | 3 | 2 |
| 21032 | 3 | 2 |
| 30876 | 4 | 2433 |
| 38424 | 5 | 2580 |
+-------+-----------+-------------+
PROPERTIES(WHERE properties.id= project_assignment.property_id)
+----+------------------+
| id | name |
+----+------------------+
| 2 | Residential |
| 3 | Multi Family |
| 34 | New Construction |
| 39 | Contemporary |
| 48 | Southwest |
+----+------------------+
I want O/P ordered by no.of projects in the list...
Residential(177) //12 - total no.of projects which is having this property
Multi Family(15)
New Construction(13)
Contemporary(11)
please give me some MySQL queries
Thank You
This should do the trick:
select
c.name,
count(c.id) as CountOfProperties
from
projects a,
project_assignments b,
properties c
where
a.ID=b.target_id
and b.property_id=c.ID
group by
c.name
order by
count(c.id) desc;
Try this::
select
prop.name,
count(prop.id) as CountOfProperties
from
projects p
inner join project_assignments pa on (p.ID=pa.target_id)
inner join properties prop on (pa.property_id=prop.ID)
group by
prop.name
order by
count(prop.id) desc;

Categories