I've a raw SQL query
SELECT cc.name, pp.population
FROM population_table pp
WHERE pp.country_name IN (
SELECT cc.name
FROM `countries` cc
)
GROUP BY pp.country_name
The above query gives the list of countries that are incountries
table along withpopulation
value.
I'm trying to convert this query to eloquent by splitting the queries into two. I'm using this eloquent in database level.
public function getCountriesWithPopulation() {
$countryTable = Country::$TABLE_NAME;
$populationTable = Population::$TABLE_NAME;
$query1 = // eloquent query
$query2 = // eloquent query
}
How can I convert the raw query into eloquent by splitting the queries?
SQL Query
SELECT country, region, area, pp.population
FROM population_table pp
FROM (
SELECT cc.country, cc.region, cc.area
FROM `countries` cc
)
GROUP BY country
You don't need to run two queries. Just consider it as a straight JOIN:
SELECT cc.name, pp.population
FROM population_table AS pp
JOIN `countries` AS cc
ON (cc.name = pp.country_name)
GROUP BY pp.country_name
And Eloquent has a built-in support for JOIN. In this case, also, theGROUP BY
is unnecessary since only one country'sname
percountry
is ever retrieved.
Actually, though, if the database is in a reasonable state (there is 1:1 correspondence betweencountries
andpopulation
) you would not even needcc
at all. The countryname
is, unlikely enough, available inpp
(it should rather be a foreign key intocountries
), so as things stand, you might just do:
SELECT country_name, population
FROM population_table;
Unless you have thepopulation
table storicized. Then things are easy for aJOIN
, more difficult for Eloquent, and there is the possibility of a missing census year inpopulation
:
SELECT cc.name, pp.population
FROM population_table AS pp
JOIN `countries` AS cc
ON (
cc.name = pp.country_name AND
pp.year=$selectedYear
)
GROUP BY pp.country_name
Basen on your query,
SELECT cc.name, pp.population
FROM population_table pp
WHERE pp.country_name IN (SELECT cc.name FROM `countries` cc)
GROUP BY pp.country_name
You can achieve the same result with Eloquent such as
public function getCountriesWithPopulation()
{
// get country names as array
$countryNames = Country::pluck('name');
// get your result
$result = Population::whereIn('country_name', $countryNames)->groupBy('country_name');
}
Edit:
matiaslauriti's comment was right, you can directly usepluck('name')
instead ofall()->pluck('name')
for efficiency and performance.
Country::pluck('name')
will return allname
column data in the country table, so that you can use later for filtering population data usingwhereIn()
Population::whereIn('country_name', $countryNames)->groupBy('country_name')
will return all data in population table where columncountry_name
is in$countryNames
array. The query will be grouped bycountry_name
column so that there is no country name duplication.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.