简体   繁体   中英

mysql query to select data from three tables

wall_paths (table)

wall_paths

wall_dimensions

在此处输入图片说明

dimensions (reference table)

在此处输入图片说明

What I've got so far:

Query

SELECT wall_paths.wall_id, wall_paths.wall_path, 
       dimensions.width, dimensions.height
FROM wall_paths
LEFT JOIN wall_dimensions
ON wall_paths.wall_id = wall_dimensions.wall_id
LEFT JOIN dimensions
ON wall_dimensions.dimension_id = dimensions.dimension_id
WHERE wall_paths.wall_id = 4;

Result

在此处输入图片说明

The query is selecting redundant rows, I just would like to select the two paths along with the corresponding dimensions based on the wall_dimensions table. Something like below:

Expected result

在此处输入图片说明

Result with GROUP BY in query

在此处输入图片说明

Please help on how to select something like the above.

Note: I've tried DISTINCT as well on the query but returns an error.

The query results are as expected, actually. Makes sense when you see it, data-wise. The data model is setup incorrectly for this query.

Fiddle (original data model): http://sqlfiddle.com/#!2/8c1bf/1

Fiddle (modified data model): http://sqlfiddle.com/#!2/3d9b0/5

Not saying you should change your model, just pointing out the query runs as expected with the current schema.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM