I don’t think this is a simple LIMIT query.
I have organisers that have categories.
Each organiser can only have 3 categories.
My problem is that I have a page to edit their categories and if I use a regular INSERT, then it may exceed 3 categories.
- Some organisers may have 2 categories, and then choose to update to have 3.
- Some organisers can have 3 categories, and update to have 2.
- An organiser must have at least 1 category (this is handled on the client side).
EDIT:
My current solution currently implemented solution is to delete all categories before inserting the modified ones, then using the client-side to restrict the limit to 3. I’m using MySQL. The database is normalised like so:
organiser organiser_categories
+-------------------+ +-------------------+--------------+
| organiser_email | | organiser_email | category_id |
+-------------------+ +-------------------+--------------+
| abc@gmail.com | | abc@gmail.com | 1 |
| xyz@gmail.com | | abc@gmail.com | 2 |
+-------------------+ +-------------------+--------------+
categories
+---------------+---------------+
| category_id | category_name |
+---------------+---------------+
| 1 | Leisure |
| 2 | History |
+---------------+---------------+
I do think you could do the checking (limit to 3 category) at the app site instead of at mysql. That is part of the verification of data.
When updating to database, i do agree with you to delete all categories and inserting the categories.