1

创建查询以列出所有乘客类型轮胎的 Store_Name、Part_ID、Model_Name、Manufacturer_Name 和 OnHand。按制造商名称对列表进行排序。

这是我放下的:

SELECT
  STORELOCATION.STORE_NAME,
  TIRES.PART_ID,
  MODEL_NAME,
  MANUFACTURERS.MANUFACTURER_NAME,
  INVENTORY.ONHAND
FROM
  STORELOCATION, TIRES, MANUFACTURERS, INVENTORY
WHERE
  TIRE_TYPE = 'Passenger'
ORDER BY MANUFACTURER_NAME;

我得到了大约 4100 条记录。我不需要重复。我得到的

+---------+--------------------+----------------+------------+-------------+
| PART_ID | MODEL_NAME         | MANUFACTURERID | UNIT_PRICE | TIRE_TYPE   |
+---------+--------------------+----------------+------------+-------------+
| C424P   | Kestral            | M3             |      45.99 | Passenger   |
| C434P   | Peregrine          | M3             |      49.99 | Passenger   |
| C435T   | Eagle              | M3             |      56.99 | Truck       |
| C475X   | Hawk               | M3             |      63.99 | Performance |
| G738P   | Cross-Country      | M1             |      27.99 | Passenger   |
| G812T   | All-Terrain        | M1             |      39.99 | Truck       |
| G814T   | Expedition         | M1             |      47.99 | Truck       |
| G868P   | Urban              | M1             |      34.99 | Passenger   |
| G898X   | Performance-Radial | M1             |      56.99 | Performance |
| M225P   | Symmetry           | M2             |      39.99 | Passenger   |
| M235P   | Harmony            | M2             |      49.99 | Passenger   |
| M325X   | Energy             | M2             |      54.99 | Performance |
| M545X   | Grand-Prix         | M2             |      89.99 | Performance |
| Y320P   | Touring            | M4             |      19.99 | Passenger   |
| Y430P   | Assurance          | M4             |      29.99 | Passenger   |
| Y435P   | Freedom            | M4             |      24.99 | Passenger   |
| Y440T   | Cargo              | M4             |      29.99 | Truck       |
| Y450T   | Heavy-Duty         | M4             |      24.99 | Truck       |
+---------+--------------------+----------------+------------+-------------+
18 rows in set (0.00 sec)

Query OK, 0 rows affected (0.06 sec)

Query OK, 4 rows affected (0.03 sec)
Records: 4  Duplicates: 0  Warnings: 0

+----------------+-------------------+-------------------+------------------+
| MANUFACTURERID | MANUFACTURER_NAME | MANUFACTURER_CITY | MANUFACTURER_REP |
+----------------+-------------------+-------------------+------------------+
| M1             | GoodTread         | Akron             | Ben              |
| M2             | Michelle          | Columbus          | Sarah            |
| M3             | ChickenCoop       | Findlay           | George           |
| M4             | Yomama            | Toledo            | Steve            |
+----------------+-------------------+-------------------+------------------+
4 rows in set (0.00 sec)

Query OK, 0 rows affected (0.07 sec)

Query OK, 3 rows affected (0.02 sec)
Records: 3  Duplicates: 0  Warnings: 0

+----------+-------------+-----------+---------------+
| STORE_ID | STORE_NAME  | LOCATION  | STORE_MANAGER |
+----------+-------------+-----------+---------------+
| DT       | LUCKY ONE   | Downtown  | Robert        |
| ES       | LUCKY TWO   | Eastside  | Megan         |
| NS       | LUCKY THREE | Northside | Harold        |
+----------+-------------+-----------+---------------+
3 rows in set (0.00 sec)

Query OK, 0 rows affected (0.13 sec)

+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| PART_ID  | char(5)      | NO   | PRI |         |       |
| STORE_ID | char(3)      | NO   | PRI |         |       |
| ONHAND   | decimal(5,0) | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+

-> FROM INVENTORY;
+---------+----------+--------+
| PART_ID | STORE_ID | ONHAND |
+---------+----------+--------+
| C424P   | DT       |      8 |
| C424P   | ES       |     28 |
| C424P   | NS       |     18 |
| C434P   | DT       |     10 |
| C434P   | ES       |     20 |
| C434P   | NS       |     10 |
| C435T   | DT       |      2 |
| C435T   | ES       |     12 |
| C475X   | DT       |     24 |
| C475X   | ES       |     20 |
| C475X   | NS       |     24 |
| G738P   | DT       |     12 |
| G738P   | ES       |     32 |
| G738P   | NS       |     20 |
| G812T   | DT       |     40 |
| G812T   | ES       |     40 |
| G812T   | NS       |     34 |
| G814T   | DT       |     20 |
| G814T   | ES       |     20 |
| G814T   | NS       |     20 |
| G868P   | DT       |     36 |
| G898X   | DT       |     12 |
| G898X   | NS       |     12 |
| M225P   | DT       |      8 |
| M225P   | NS       |      8 |
| M235P   | DT       |      4 |
| M235P   | NS       |      4 |
| M325X   | DT       |     40 |
| M325X   | ES       |     40 |
| M325X   | NS       |     50 |
| M545X   | DT       |     40 |
| M545X   | NS       |     60 |
| Y320P   | DT       |     12 |
| Y320P   | ES       |     12 |
| Y320P   | NS       |     32 |
| Y430P   | DT       |     44 |
| Y430P   | ES       |     44 |
| Y430P   | NS       |     44 |
| Y435P   | DT       |     12 |
| Y435P   | ES       |     20 |
| Y435P   | NS       |     12 |
| Y440T   | DT       |      8 |
| Y450T   | DT       |     32 |
| Y450T   | ES       |     36 |
| Y450T   | NS       |     32 |
+---------+----------+--------+
4

3 回答 3

2

您在当前查询中遇到的问题是您没有将表连接到适当的外键,因此您正在生成笛卡尔结果。

使用您发布的架构,您需要JOIN外键上的表:

SELECT
  s.STORE_NAME,
  t.PART_ID,
  t.MODEL_NAME,
  m.MANUFACTURER_NAME,
  i.ONHAND
FROM STORELOCATION s
INNER JOIN INVENTORY i
  on s.STORE_ID = i.storeid
INNER JOIN TIRES t
  on i.partid = t.PART_ID
INNER JOIN MANUFACTURERS m
  on t.MANUFACTURERID = m.MANUFACTURERID
WHERE TIRE_TYPE = 'Passenger'
ORDER BY MANUFACTURER_NAME;

请参阅带有演示的 SQL Fiddle

于 2013-05-02T21:35:43.287 回答
1

假设从您的查询反向工程的最小架构:

storelocation
    store_location_id
    store_name

inventory
    store_location_id
    part_id
    onhand

tires
   part_id
   manufacturer_id
   model_name

manufacturer
   manufacturer_id
   manufacturer_name

这个查询接近你想要的

SELECT
  sl.STORE_NAME,
  t.PART_ID,
  t.MODEL_NAME,
  m.MANUFACTURER_NAME,
  inv.ONHAND
FROM
  inventory inv 
    left join storelocation sl on inv.store_location_id = sl.store_location_id
    left join tires t on inv.part_id = t.part_id
    left join manufacturer m on t.manufacturer_id = m.manufacturer_id
WHERE
  t.TIRE_TYPE = 'Passenger'
ORDER BY MANUFACTURER_NAME;
于 2013-05-02T21:34:53.587 回答
-2

您需要GROUP BY不希望有重复的列示例

    WHERE
    TIRE_TYPE = 'Passenger'
    GROUP BY STORELOCATION.STORE_NAME
    ORDER BY MANUFACTURER_NAME;
于 2013-05-02T21:15:04.427 回答