I'm working on an insurance comparison platform using Codeigniter. I have a packages table which looks like this:
+----------+--------------+------------+---------------------+-------------+
| tpckg_id | tpckg_people | tpckg_days | tpckg_policy_amount | tp_name |
+----------+--------------+------------+---------------------+-------------+
| 21 | individual | 10 | 1000 | Test25 |
| 22 | family | 7 | 2000 | Testing |
| 23 | individual | 20 | 600 | TPL Care |
| 24 | family | 10 | 1000 | TPL Care |
| 25 | individual | 15 | 650 | Travel Care |
| 26 | family | 10 | 1100 | TPL Care |
| 27 | individual | 7 | 500 | TPL Care |
| 28 | family | 7 | 1200 | Travel Care |
| 29 | family | 10 | 500 | TPL Care 2 |
| 30 | individual | 11 | 500 | TPL Care |
+----------+--------------+------------+---------------------+-------------+
Now, I'm fetching the data using this query:
$this->db->select('travel_packages.tpckg_id, travel_packages.tpckg_people, travel_packages.tpckg_days, travel_packages.tpckg_policy_amount, travel_products.tp_name');
$this->db->from('travel_packages');
$this->db->join('travel_products', 'travel_products.tp_id = travel_packages.tpckg_products_id')
$this->db->where('travel_packages.tpckg_people', 'Individual');
$this->db->where('travel_packages.tpckg_days >= 10');
$this->db->group_by('travel_packages.tpckg_policy_amount');
$this->db->order_by('travel_packages.tpckg_policy_amount', 'ASC');
As expected is returns all the records which has tpckg_people = individual and tpckg_days >= 10 like this:
+----------+--------------+------------+---------------------+-------------+
| tpckg_id | tpckg_people | tpckg_days | tpckg_policy_amount | tp_name |
+----------+--------------+------------+---------------------+-------------+
| 21 | individual | 10 | 1000 | Test25 |
| 23 | individual | 20 | 600 | TPL Care |
| 25 | individual | 15 | 650 | Travel Care |
| 30 | individual | 11 | 500 | TPL Care |
+----------+--------------+------------+---------------------+-------------+
What I want it to return is only the first greater number of days if there's no equal number than what user has entered. For example if user enters 9 for number of days and I have no package with 9 number of days it should return the first greater number of days of each product.
Like this:
+----------+--------------+------------+---------------------+-------------+
| tpckg_id | tpckg_people | tpckg_days | tpckg_policy_amount | tp_name |
+----------+--------------+------------+---------------------+-------------+
| 21 | individual | 10 | 1000 | Test25 |
| 25 | individual | 15 | 650 | Travel Care |
| 30 | individual | 11 | 500 | TPL Care |
+----------+--------------+------------+---------------------+-------------+
Any help with the query, please? Thanks in advance. :-)
Here's a raw query...
Select x.*
From travel_packages x
Join
( Select tp_name name
, tpckg_people people
, min(tpckg_days) min_days
from travel_packages
where tpckg_people = 'individual'
and tpckg_days >= 9
group
by name
, people
) y
On y.name = x.tp_name
And y.people = x.tpckg_people
And y.min_days = x.tpckg_days;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.