创建一个没有重复项的mysql查询

时间:2013-05-02 21:27:25

标签: mysql duplicates

创建一个查询,列出所有乘客类型轮胎的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条记录。我不需要重复。我得到了

感谢能帮助我的人Craig

+---------+--------------------+----------------+------------+-------------+
| 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    |       |
+----------+--------------+------+-----+---------+-------+

1 个答案:

答案 0 :(得分:0)

如果您有实际重复项,可以将“选择”更改为“选择不同”并完成;但是你有一个更大的问题;你的“重复”并不是真的重复

基本上,你给出的唯一限制是轮胎类型必须是“乘客”。这些轮胎如何与例如制造商名称相关?如果没有给出约束(如现在),它只返回每个可能的组合。例如,如果您想根据partid将轮胎表链接到轮胎表,您可以将where语句更改为

WHERE TIRE_TYPE = 'Passenger' AND TIRES.PART_ID = INVENTORY.PART_ID

我可能有错误的数据库名称,但我认为您了解问题所在。要解决“重复”问题,您需要在while语句中定义不同表之间的关系。