mysql inner join 2 tables and order by count - php

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;

Related

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;

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)

How to select mysql results?

invoice
+----+-----+---------+-------+
| Sr | BRN | Name | Amnt |
+----+-----+---------+-------+
| 1 | 1 | John | 10 |
| 2 | 1 | John | 4 |
| 3 | 2 | Belly | 4 |
| 4 | 3 | John | 14 |
| 5 | 4 | John | 5 |
| 6 | 4 | John | 14 |
+----+-----+---------+-------+
I want to select all rows except the duplicate BRN. (If there are two/more ge in BRN then it should only select one)
I tried:
SELECT *(DISTINCT BRN) FROM invoice
Expected result:
+-----+---------+-------+
| BRN | Name | Amnt |
+-----+---------+-------+
| 1 | John | 10 |
| 2 | Belly | 4 |
| 3 | John | 14 |
| 4 | John | 5 |
+-----+---------+-------+
Given the following table:
+----+-----+---------+-------+
| Sr | BRN | Name | Amnt |
+----+-----+---------+-------+
| 1 | 1 | John | 10 |
| 2 | 1 | John | 4 |
| 3 | 2 | Belly | 4 |
| 4 | 3 | John | 14 |
| 5 | 4 | John | 5 |
| 6 | 4 | John | 14 |
+----+-----+---------+-------+
with the expected results:
+-----+---------+-------+
| BRN | Name | Amnt |
+-----+---------+-------+
| 1 | John | 10 |
| 2 | Belly | 4 |
| 3 | John | 14 |
| 4 | John | 5 |
+-----+---------+-------+
The difficult part is getting the amount, because it is arbitrary, not to mention that the values in Amnt are pretty much worthless in this result.
If you want distinct BRN, the query would be SELECT DISTINCT BRN FROM invoice
You might even get away with SELECT DISTINCT BRN, Name FROM invoice
An intermediate step would be SELECT BRN,Name FROM invoice GROUP BY BRN, Name
But if you try to include Amnt in the equation, then the query will fail because there's no way for the database to determine which Amnt to show.
So, you could try this kludge:
SELECT a.BRN, a.Name, b.Amnt FROM invoice AS a LEFT JOIN invoice AS b ON a.BRN=b.BRN
No guarantees on which Amnt it will pick up, though.
Hope that helps.
SELECT * FROM invoice WHERE Date >= :fdate GROUP BY BRN
See Here Use GROUP BY in Query with your Conditions

MYSQL select query based on another tables multiple values

I have two tables one for units and one for Amenities
Table units
+---------+---------------------+---------------------+-----------+----------+
| unit_id | date added | date modified | unit name | user |
+---------+---------------------+---------------------+-----------+----------+
| 1 | 2001-10-29 13:11:00 | 2001-10-29 13:11:00 | Villa 1 | Smith |
| 2 | 2001-10-29 13:11:00 | 2001-10-29 13:11:00 | Villa 2 | Smith |
| 3 | 2001-10-29 13:11:00 | 2001-10-29 13:11:00 | Villa 3 | Jones |
| 4 | 2001-10-29 13:11:00 | 2001-10-29 13:11:00 | Apartment | Smith |
+---------+---------------------+---------------------+-----------+----------+
Table Amenities
+---------+-----------+-------------------+
| id | Unit_id | Amenity |
+---------+-----------+-------------------+
| 1 | 1 | Air conditions |
| 2 | 1 | Internet |
| 3 | 1 | Water heaters |
| 4 | 1 | TV |
| 5 | 2 | TV |
| 6 | 2 | pool |
| 7 | 2 | Internet |
| 8 | 3 | Internet |
| 9 | 4 | Internet |
+---------+-----------+-------------------+
i want to select units where it has both TV and Internet
I try
select units.* from units left join Amenities on units.unit_id=Amenities.Unit_id
where Amenities.Amenity='TV' and Amenities.Amenity='Internet'
but not work
DROP TABLE IF EXISTS amenities;
CREATE TABLE amenities
(unit_id INT NOT NULL
,amenity VARCHAR(50) NOT NULL
,PRIMARY KEY(unit_id,amenity)
);
INSERT INTO amenities VALUES
(1,'Air conditions'),
(1,'Internet'),
(1,'Water heaters'),
(1,'TV'),
(2,'TV'),
(2,'pool'),
(2,'Internet'),
(3,'Internet'),
(4,'Internet');
SELECT unit_id
FROM amenities
WHERE amenity IN ('TV','Internet')
GROUP
BY unit_id
HAVING COUNT(*) = 2;
+---------+
| unit_id |
+---------+
| 1 |
| 2 |
+---------+
Obviously, you'd never have a table like the one you describe. Instead, you'd have a table of unit, a table of amenities, and a table which says which amenity belongs to which unit.
SELECT ua.*
FROM units AS ua
INNER JOIN units AS ub ON ua.unit_id = ub.unit_id
INNER JOIN Amenities AS aa
ON ua.unit_id = aa.Unit_id
AND aa.Amenity = 'TV'
INNER JOIN Amenities AS ab
ON ub.unit_id = ab.Unit_id
AND ab.Amenity = 'Internet';
Here's the output generated with your data:
+---------+---------------------+---------------------+-----------+-------+
| unit_id | date added | date modified | unit name | user |
+---------+---------------------+---------------------+-----------+-------+
| 1 | 2001-10-29 13:11:00 | 2001-10-29 13:11:00 | Villa 1 | Smith |
| 2 | 2001-10-29 13:11:00 | 2001-10-29 13:11:00 | Villa 2 | Smith |
+---------+---------------------+---------------------+-----------+-------+

SQL select count and sum from different tables

I need help to resolve this.
I have 4 tables :
Transactions
| id | cid | gt | rt |
| 1 | 6 | 2 | 5 |
| 2 | 6 | 9 | 7.5 |
| 3 | 6 | 3 | 9.7 |
| 4 | 3 | 3 | 7.0 |
| 5 | 3 | 7 | 6.8 |
| 6 | 9 | 4 | 2.5 |
| 7 | 9 | 2 | 5.4 |
Clients
| id | firstname | lastname | date |
| 1 | jean | moulin | 1987 |
| 2 | salah | fera | 1968
| 3 | marouan | youra | 2001 |
| 4 | amin | esa | 1963 |
| 5 | kamal | tara | 1789 |
| 6 | moad | mara | 2005 |
| 9 | safaa | dara | 2004 |
Produit A
| id | cid |
| 1 | 6 |
| 2 | 6 |
| 3 | 3 |
| 4 | 3 |
| 5 | 3 |
| 6 | 4 |
| 7 | 1 |
Produit B
| id | cid |
| 1 | 6 |
| 2 | 3 |
| 3 | 9 |
| 4 | 3 |
| 5 | 3 |
| 6 | 4 |
| 7 | 6 |
The result that i need is :
cid | name | date | pa | pb | gt | rt |
3 | | | | | | |
6 | | | | | | |
9 | | | | | | |
I need to select from transaction all distinct client id (pid) and select the firstname and last name (name = firstname lastname) and date from clients table and sum all values (gt) and (rt) and search in table produitA the number of products for this client by his id and the same thing for the table produitB.
What i do for this but it don't work is (suggested by Gimeniux):
SELECT
clients.id,
CONCAT(firstname, ' ', lastname) as name,
date,
count(distinct produitA.id) as pa,
count(distinct produitB.id) as pb,
sum(gt) AS gt,
sum(rt) AS rt
FROM clients
LEFT JOIN transactions ON clients.id = transactions.pid
LEFT JOIN produitA ON clients.id = produitA.cid
LEFT JOIN produitB ON clients.id = produitB.cid
where pid is not null
group by clients.id
The probleme here is that gt and rt values are true for only the first client. For the second client and third and ... there is different values that are not true.
Although is hard for me to see the logic between your tables, you can use this query to get the result you desire. But i think it won't work if there are two same 'gt' or two same 'rt' values for one client.
SELECT
clients.id,
CONCAT(firstname, ' ', lastname) as name,
date,
count(distinct produitA.id) as pa,
count(distinct produitB.id) as pb,
sum(distinct gt) AS gt,
sum(distinct rt) AS rt
FROM clients
LEFT JOIN transactions ON clients.id = transactions.pid
LEFT JOIN produitA ON clients.id = produitA.cid
LEFT JOIN produitB ON clients.id = produitB.cid
where pid is not null
group by clients.id
Row for pid=9 doesn't show because in the data you gave there is no client with id=9

Categories