I have the follow two mysql tables:
mysql> desc subscriptions;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(6) | NO | PRI | NULL | auto_increment |
| id_user | int(6) | NO | MUL | NULL | |
| id_package | int(4) | NO | | NULL | |
| expiry | date | YES | | NULL | |
| ip | varchar(100) | NO | | NULL | |
| amount | double | NO | | NULL | |
| processed | tinyint(4) | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
mysql> desc packages;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(6) | NO | PRI | NULL | auto_increment |
| id_panel | tinyint(4) | NO | | NULL | |
| name | varchar(100) | NO | | NULL | |
| price | double | NO | | NULL | |
| period | varchar(50) | NO | | NULL | |
| days | int(11) | NO | | NULL | |
| visible | tinyint(4) | NO | | NULL | |
| payment | tinyint(4) | NO | | 1 | |
+----------+--------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)
I’m using the follow query to retrive all the packages and the relative USER expirations date (of the specific package).
SELECT packages.id, packages.name, packages.price, packages.period, packages.days, MAX(subscriptions.expiry) as expiry
FROM packages LEFT JOIN subscriptions
ON subscriptions.id_package = packages.id AND subscriptions.processed = 1 AND subscriptions.id_user = 1
WHERE packages.visible = 1
GROUP BY subscriptions.id_user
ORDER BY packages.id_panel
My problem is that if there is not the subscription of a specific package the query does not show the package.
I MUST show all the page and….IF the user has a subscription check the MAX expiry date (becase i track all the subscrions the user made).
How can i show all the packages and the relative user expiration dates? (and if there is no subscription of a package show NULL)
You’re grouping by
subscriptions.id_user, yet you select all your non_aggregates from thepackagestable. Are you sure that you’re grouping by the correct field?It looks like it should be
packages.id…