Mysql 在sql中的不同表中查找同一列

Mysql 在sql中的不同表中查找同一列,mysql,sql,database,Mysql,Sql,Database,我有一个小问题,我有四个表,我使用的是mysql数据库 此处的测量表将创建所有测量 ╔══════════╤════════╗ ║ SurveyId │ Name ║ ╠══════════╪════════╣ ║ 1 │ First ║ ╟──────────┼────────╢ ║ 2 │ Second ║ ╚══════════╧════════╝ 仪表类别表 要将两个仪表类别表与测量表连接 我使用另一个名为Title table的表 标题表 此处,ti

我有一个小问题,我有四个表,我使用的是mysql数据库

此处的测量表将创建所有测量

╔══════════╤════════╗
║ SurveyId │ Name   ║
╠══════════╪════════╣
║ 1        │ First  ║
╟──────────┼────────╢
║ 2        │ Second ║
╚══════════╧════════╝
仪表类别表

要将两个仪表类别表与测量表连接

我使用另一个名为Title table的表

标题表

此处,title1、title2和title3是量规类别表的外键,surveyId是测量表的外键

另一个名为Average_values的表具有标题表的相应值

平均值表

我的问题是,如果我想从Average_values表中得到应力值,我如何才能得到它?因为外键不正常。输出应为23,56,28。有什么办法得到它吗?我还有另一个想法,将Average_values表修改为

并将这些值逐一放入,此处CategoryId和SurveyID分别处于量规类别表和测量表的外键关系中。但我觉得这不是一张有效的桌子。因为需要动态处理大量数据。

如果gauge1、gauge2、gauge3表示相同的概念,则没有理由创建多个字段

这是数据库设计中的一个“错误”

您应该规范化您的表,并为每个值指定一行。 关系数据库引擎就是这样设计的。
查看internet上的关系数据库规范化,了解更多信息

我觉得您的表设计很奇怪,在我看来,其中两个表需要取消设置,以删除某些字段上的1,2,3后缀。至于你剩下的问题是什么,这仍然是个谜。没有明确的方法连接到一个名为Guage的表,但有一个CategoryID键。下面是一个执行unpivot并尝试连接所有表的查询

MySQL 5.6架构设置:

问题1:

:


您是哪个数据库Using@AlfaizAhmedmysqlcategory表如何与其他表相关?我找不到任何其他的分类table@kiks73很抱歉,我刚刚编辑了Average_values表,请检查Average_values表没有任何CategoryId字段或映射以获取此信息。您需要更新表中的CategoryId。是的,我遇到了问题,所以我的替代方案好吗?意思是,必须把一个接一个的关系用categoryId
╔════════════╤═════════════╗
║ CategoryId │ Title       ║
╠════════════╪═════════════╣
║ 1          │ Stress      ║
╟────────────┼─────────────╢
║ 2          │ Environment ║
╟────────────┼─────────────╢
║ 3          │ Health      ║
╚════════════╧═════════════╝
╔══════════════╤════════╤════════╤════════╤══════════╗
║ GaugeTitleId │ title1 │ title2 │ title3 │ surveyId ║
╠══════════════╪════════╪════════╪════════╪══════════╣
║ 1            │ 2      │ 3      │ 1      │ 1        ║
╟──────────────┼────────┼────────┼────────┼──────────╢
║ 2            │ 1      │ 3      │ 2      │ 1        ║
╟──────────────┼────────┼────────┼────────┼──────────╢
║ 3            │ 3      │ 1      │ 2      │ 2        ║
╚══════════════╧════════╧════════╧════════╧══════════╝
╔═════════╤════════╤════════╤════════╤══════════╤══════════════╗
║ GaugeID │ Gauge1 │ Guage2 │ Gauge3 │ SurveyId │ GaugeTitleId ║
╠═════════╪════════╪════════╪════════╪══════════╪══════════════╣
║ 1       │ 34     │ 76     │ 23     │ 1        │ 1            ║
╟─────────┼────────┼────────┼────────┼──────────┼──────────────╢
║ 2       │ 56     │ 23     │ 67     │ 1        │ 1            ║
╟─────────┼────────┼────────┼────────┼──────────┼──────────────╢
║ 3       │ 14     │ 28     │ 56     │ 1        │ 2            ║
╚═════════╧════════╧════════╧════════╧══════════╧══════════════╝
╔═════════╤════════╤════════════╤══════════╗
║ GuageId │ values │ CategoryId │ SurveyId ║
╚═════════╧════════╧════════════╧══════════╝
CREATE TABLE Survey 
    (`SurveyId` int, `Name` varchar(6))
;

INSERT INTO Survey 
    (`SurveyId`, `Name`)
VALUES
    (1, 'First'),
    (2, 'Second')
;


CREATE TABLE Gauge 
    (`CategoryId` int, `Title` varchar(11))
;

INSERT INTO Gauge 
    (`CategoryId`, `Title`)
VALUES
    (1, 'Stress'),
    (2, 'Environment'),
    (3, 'Health')
;


CREATE TABLE Title 
    (`GaugeTitleId` int, `title1` int, `title2` int, `title3` int, `surveyId` int)
;

INSERT INTO Title 
    (`GaugeTitleId`, `title1`, `title2`, `title3`, `surveyId`)
VALUES
    (1, 2, 3, 1, 1),
    (2, 1, 3, 2, 1),
    (3, 3, 1, 2, 2)
;


CREATE TABLE Average_values 
    (`GaugeID` int, `Gauge1` int, `Guage2` int, `Gauge3` int, `SurveyId` int, `GaugeTitleId` int)
;

INSERT INTO Average_values 
    (`GaugeID`, `Gauge1`, `Guage2`, `Gauge3`, `SurveyId`, `GaugeTitleId`)
VALUES
    (1, 34, 76, 23, 1, 1),
    (2, 56, 23, 67, 1, 1),
    (3, 14, 28, 56, 1, 2)
;
select
        t.surveyId
      , t.GaugeTitleId
      , g.title Gauge_Title
      , case when cj.n = 1 then t.title1 
             when cj.n = 2 then t.title2
             when cj.n = 3 then t.title3
        end Title
      , case when cj.n = 1 then av.Gauge1 
             when cj.n = 2 then av.Guage2
             when cj.n = 3 then av.Gauge3
        end Gauge
from Title t
cross join (
  select 1 n union all
  select 2 n union all
  select 3 n) cj
inner join Average_values av on t.surveyId = av.surveyId
                            and t.GaugeTitleId = av.GaugeTitleId
inner join Gauge g on t.GaugeTitleId = g.CategoryId  
where g.title = 'Stress'
order by Title, Gauge
| surveyId | GaugeTitleId | Gauge_Title | Title | Gauge |
|----------|--------------|-------------|-------|-------|
|        1 |            1 |      Stress |     1 |    23 |
|        1 |            1 |      Stress |     1 |    67 |
|        1 |            1 |      Stress |     2 |    34 |
|        1 |            1 |      Stress |     2 |    56 |
|        1 |            1 |      Stress |     3 |    23 |
|        1 |            1 |      Stress |     3 |    76 |