我需要幫助獲取MySQL中結果的第n個數字。無法獲得MySQL中的第n個結果
我有三個表具有以下字段:
table1: product_id, name, description
table2: category_id, name, description
table3: product_id, category_id
我想要得到這樣的結果:如下
name | description - product_category -category2 - category3
___________________________________________________________________
NAME 1 | Description 1 | Cat1 | Cat2 | Cat5
NAME 2 | Description 2 | Cat7 | Cat9 | Cat11
我試圖使用的查詢是:
SELECT
t1.name AS product_name,
t1.description AS product_description,
(SELECT t2.name from table2 order by name ASC LIMIT 1
OFFSET 1) AS product_category,
(SELECT t2.name from table2 order by name ASC LIMIT 1
OFFSET 2) AS product_category2,
(SELECT t2.name from table2 order by name ASC LIMIT 1
OFFSET 3) AS product_category3
FROM table1 t1 INNER JOIN
table3 t3
ON t3.product_id = t1.product_id INNER JOIN
table2 t2
ON t2.category_id = t3.category_id
;
不幸地我得到了這個結果:
name - description - category
NAME 1 - Description 1 - Category 1
NAME 2 - Description 2 - Category 2
你真的不表明爲什麼結果不是你所期望的。 –
您沒有任何頂層'ORDER BY'子句,所以'nnth'的概念就是MySQL希望它成爲的任何東西。 –
事情是應該有更多的類別在描述中提到,但只有一個返回,所以基本上MYSQL獲得第一個結果,然後停止尋找更多。 – jukerok