简体   繁体   English

mysql查询从三个表中选择数据

[英]mysql query to select data from three tables

wall_paths (table) wall_paths(表)

wall_paths

wall_dimensions 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. 该查询正在选择多余的行,我只想基于wall_dimensions表选择两条路径以及相应的尺寸。 Something like below: 如下所示:

Expected result 预期结果

在此处输入图片说明

Result with GROUP BY in query 查询中使用GROUP BY结果

在此处输入图片说明

Please help on how to select something like the above. 请帮助如何选择上述内容。

Note: I've tried DISTINCT as well on the query but returns an error. 注意:我在查询中也尝试过DISTINCT ,但返回错误。

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 小提琴(原始数据模型): http ://sqlfiddle.com/#!2/8c1bf/1

Fiddle (modified data model): http://sqlfiddle.com/#!2/3d9b0/5 小提琴(修改后的数据模型): 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. 并不是说您应该更改模型,只是指出查询在当前模式下按预期运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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