Here’s my data sample:
product -------------------------------- | product_id PK | product_name | -------------------------------- | FRT1 | Apple | | FRT2 | Orange | | TXT1 | Ribbon | -------------------------------- package ------------------------- | package_id PK | price | ------------------------- | P001 | 1000 | | P002 | 1500 | | P003 | 1000 | ------------------------- package_detail --------------------------------- | package_id FK | product_id FK | --------------------------------- | P001 | FRT1 | | P002 | FRT1 | | P002 | FRT2 | | P003 | FRT2 | ---------------------------------
The key for searching is product_name, so if I search “Apple”, result should be like this:
------------------------------------------- | package_id | price | num_of_package_dtl | ------------------------------------------- | P001 | 1000 | 1 | | P002 | 1500 | 2 | -------------------------------------------
I’m weak at joining, can anyone help me with the query?
Thank you in advance~
SQLFiddle Demo