|
| 1 | +[](https://github.com/javadev/LeetCode-in-Kotlin) |
| 2 | +[](https://github.com/javadev/LeetCode-in-Kotlin/fork) |
| 3 | + |
| 4 | +## 3465\. Find Products with Valid Serial Numbers |
| 5 | + |
| 6 | +Easy |
| 7 | + |
| 8 | +Table: `products` |
| 9 | + |
| 10 | + +--------------+------------+ |
| 11 | + | Column Name | Type | |
| 12 | + +--------------+------------+ |
| 13 | + | product_id | int | |
| 14 | + | product_name | varchar | |
| 15 | + | description | varchar | |
| 16 | + +--------------+------------+ |
| 17 | + (product_id) is the unique key for this table. |
| 18 | + Each row in the table represents a product with its unique ID, name, and description. |
| 19 | + |
| 20 | +Write a solution to find all products whose description **contains a valid serial number** pattern. A valid serial number follows these rules: |
| 21 | + |
| 22 | +* It starts with the letters **SN** (case-sensitive). |
| 23 | +* Followed by exactly `4` digits. |
| 24 | +* It must have a hyphen (-) **followed by exactly** `4` digits. |
| 25 | +* The serial number must be within the description (it may not necessarily start at the beginning). |
| 26 | + |
| 27 | +Return _the result table ordered by_ `product_id` _in **ascending** order_. |
| 28 | + |
| 29 | +The result format is in the following example. |
| 30 | + |
| 31 | +**Example:** |
| 32 | + |
| 33 | +**Input:** |
| 34 | + |
| 35 | +products table: |
| 36 | + |
| 37 | + +------------+--------------+------------------------------------------------------+ |
| 38 | + | product_id | product_name | description | |
| 39 | + +------------+--------------+------------------------------------------------------+ |
| 40 | + | 1 | Widget A | This is a sample product with SN1234-5678 | |
| 41 | + | 2 | Widget B | A product with serial SN9876-1234 in the description | |
| 42 | + | 3 | Widget C | Product SN1234-56789 is available now | |
| 43 | + | 4 | Widget D | No serial number here | |
| 44 | + | 5 | Widget E | Check out SN4321-8765 in this description | |
| 45 | + +------------+--------------+------------------------------------------------------+ |
| 46 | + |
| 47 | +**Output:** |
| 48 | + |
| 49 | + +------------+--------------+------------------------------------------------------+ |
| 50 | + | product_id | product_name | description | |
| 51 | + +------------+--------------+------------------------------------------------------+ |
| 52 | + | 1 | Widget A | This is a sample product with SN1234-5678 | |
| 53 | + | 2 | Widget B | A product with serial SN9876-1234 in the description | |
| 54 | + | 5 | Widget E | Check out SN4321-8765 in this description | |
| 55 | + +------------+--------------+------------------------------------------------------+ |
| 56 | + |
| 57 | +**Explanation:** |
| 58 | + |
| 59 | +* **Product 1:** Valid serial number SN1234-5678 |
| 60 | +* **Product 2:** Valid serial number SN9876-1234 |
| 61 | +* **Product 3:** Invalid serial number SN1234-56789 (contains 5 digits after the hyphen) |
| 62 | +* **Product 4:** No serial number in the description |
| 63 | +* **Product 5:** Valid serial number SN4321-8765 |
| 64 | + |
| 65 | +The result table is ordered by product\_id in ascending order. |
| 66 | + |
| 67 | +## Solution |
| 68 | + |
| 69 | +```sql |
| 70 | +# Write your MySQL query statement below |
| 71 | +SELECT * FROM products WHERE description REGEXP 'SN[0-9]{4}-[0-9]{4}$' |
| 72 | +OR description REGEXP 'SN[0-9]{4}-[0-9]{4}[^0-9]+' ORDER BY product_id |
| 73 | +``` |
0 commit comments