I currently have around 1,000 products in a MySQL table, each of which appears to have been duplicated (1 with a product SKU and one with a product MPN, or Manufacturer Product Number), as shown below:
+--------------+------------+----------------+-----------------+------------------------+
| V_Article_ID | Article_ID | V_Article_Code | Brand_Domain_ID | Prefered_V_Article_ID |
+--------------+------------+----------------+-----------------+------------------------+
| 11003 | 7043 | AA4011 | null | 11002 |
| 11002 | 7043 | U-30G-BK | 101036 | null |
+--------------+------------+----------------+-----------------+------------------------+
Ideally what i would like is to combine the two rows into one row for EACH product (as you can see the article_id is the same for both rows), keep the Brand_Domain_ID that is NOT NULL and split the two separate Vendor_Article_Code's into two separate columns; SKU (AA4011) and MPN (U-30G-BK), so the output would looks something like this:
+------------+--------+----------+-----------------+
| Article_ID | SKU | MPN | Brand_Domain_ID |
+------------+--------+----------+-----------------+
| 7043 | AA4011 | U-30G-BK | 101036 |
+------------+--------+----------+-----------------+
I am currently using the following statement to achieve what i want (if only for a single product based on Article_ID):
SELECT article_id,
case WHEN v_article_code REGEXP '^AA' THEN v_article_code END as SKU,
case WHEN v_article_code NOT REGEXP '^AA' THEN v_article_code END as MPN,
Brand_Domain_ID
FROM vendor_article
WHERE article_id = 7043
+------------+--------+----------+-----------------+
| Article_ID | SKU | MPN | Brand_Domain_ID |
+------------+--------+----------+-----------------+
| 7043 | null | U-30G-BK | null |
+------------+--------+----------+-----------------+
| 7043 | AA4011 | null | 101036 |
+------------+--------+----------+-----------------+
My question is this: Is there a way to cut this down so that both rows become a single row, with no null elements and the same Article_ID? Also, is there a quick way in which to iterate through each of the Article_IDs, such as a for loop in PHP? I know how to insert the output into a new table, i'm just not sure the best way to approach the solution.
Any help would be most appreciated.