2

我的广告门户有 4 个表。首先主表名是ads

+-------------+
| Field       +
+-------------+
| id      +
| ads_title   +
+-------------+

广告表的示例数据;

+----+-----------------+
| id | ads_title       |
+----+-----------------+
| 20 | 2006 CITROEN C4 |
| 27 | EMEKLI OGRETMEN |
| 28 | Harika 10 n     |
| 34 | HATASIZ BOYASIZ |
| 49 | BAYANDAN 2009   |
+----+-----------------+

用于存储在表格中的广告规格。表名是ads_spc

+---------+
| Field   |
+---------+
| id      |
| key_name|
+---------+

ads_spc 表的数据示例;

+----+-------------+
| id | key_name    |
+----+-------------+
|  1 | Date        |
|  2 | Km          |
|  3 | Colr        |
|  4 | Engine      |
|  5 | Pw. Engine  |
|  6 | Oil         |
|  7 | Speed       |
|  8 | Boody Type  |
|  9 | Traction    |
| 10 | Warranty    |
+----+-------------+

最后按 ads_id 存储 ads_spc 的规格值。表名是ads_values

+------------+
| Field      |
+------------+
| id         |
| ads_id     |
| spc_id     |
| value      |
+------------+

例如这个表数据

+----+--------+--------+-------+
| id | ads_id | spc_id | value |
+----+--------+--------+-------+
| 25 |     49 |      9 |  2119 |
| 26 |     49 |     10 |  2131 |
| 27 |     34 |      1 |  2010 |
| 28 |     34 |      2 |  8900 |
| 29 |     34 |      3 |     4 |
| 30 |     34 |      4 |    22 |
| 31 |     34 |      5 |    40 |
| 32 |     34 |      6 |    60 |
| 33 |     34 |      7 |    65 |
| 34 |     34 |      8 |    66 |
+----+--------+--------+-------+

我想在数据行中使用这些表格列出广告(表格:广告)和规格(表格:ads_spc)和广告值(表格:ads_values)。

联合到; ADS + ADS_SPC + ADS_VALUES(所有值均可搜索)

我正在使用这种情况:

SELECT SQL_CALC_FOUND_ROWS 
*
FROM ads AS a
LEFT JOIN ads_spc AS aspc 
LEFT JOIN ads_values AS aval ON aval.ads_id=a.id AND aval.spc_id=aspc.spc_id

我怎样才能联合到一排?对不起我的英语不好。

4

1 回答 1

1

为什么联合没有意义的联合你应该加入这些表

SELECT v.id, a.ads_title,s.key_name,v.value FROM `ads` a
INNER JOIN `ads_values`  v ON (a.id =v.ads_id)
INNER JOIN `ads_spc` s ON(v.spc_id =s.id) WHERE s.key_name='Color'
于 2013-08-07T20:57:58.867 回答