Create a query on mysql -
create query list store_name, part_id, model_name, manufacturer_name, , onhand passenger type tires. sort list manufacturer name.
this put down:
select storelocation.store_name, tires.part_id, model_name, manufacturers.manufacturer_name, inventory.onhand storelocation, tires, manufacturers, inventory tire_type = 'passenger' order manufacturer_name;
i got 4100 records. need no duplicates. got
+---------+--------------------+----------------+------------+-------------+ | 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 1 | downtown | robert | | es | lucky 2 | eastside | megan | | ns | lucky 3 | northside | harold | +----------+-------------+-----------+---------------+ 3 rows in set (0.00 sec) query ok, 0 rows affected (0.13 sec) +----------+--------------+------+-----+---------+-------+ | field | type | null | key | default | | +----------+--------------+------+-----+---------+-------+ | part_id | char(5) | no | pri | | | | store_id | char(3) | no | pri | | | | onhand | decimal(5,0) | yes | | null | | +----------+--------------+------+-----+---------+-------+ -> 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 | +---------+----------+--------+
the problem having in current query not joining tables appropriate foreign keys result generating cartesian result.
using schema posted, need join
tables on foreign keys:
select s.store_name, t.part_id, t.model_name, m.manufacturer_name, i.onhand storelocation s inner join inventory 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 tire_type = 'passenger' order manufacturer_name;
see sql fiddle demo
Comments
Post a Comment