[英]SQL query with table structure
I have a database table structure as follow: 我有一个数据库表结构,如下所示:
datatype
data
mytable
now datatype has primary key datatypeid, data table has datatypeid as foriegn key and has mytable id as foreign key. 现在,数据类型具有主键datatypeid,数据表具有datatypeid作为foreign键,而mytable id作为外键。
In datatype I have a column called description which has address1, address2 etc 在数据类型中,我有一列称为description,其中包含地址1,地址2等
And the values for these addresses are stored in data table using the datatypeid as reference. 这些地址的值使用datatypeid作为参考存储在数据表中。
So I want to make a select statement to show the ids from mytable and address1, address2 etc as columns, with the values coming from data.values
as follow: 所以我想做一条选择语句,将mytable和address1,address2等的ID列显示,其值来自
data.values
,如下所示:
id address1 address2
1 test add test add2
2 test add test add2
2 test add test add2
here is my query so far but it outputs it in different format: 到目前为止,这是我的查询,但是它以不同的格式输出:
SELECT id
FROM datatype
INNER JOIN data ON datatype.DataTypeID = data.DataTypeID
INNER JOIN mytable ON data.ID = mytable.ID
EDIT : 编辑:
table structure: 表结构:
datatype
-datatypeid int
-description varchar(50)
data
-id int
-datatypeid int
-datavalue varchar(50)
-mytableid int
mytable
-mytableid int
-datecreated datetime
Sample Data 样本数据
datatype :
datatypeid description
1 address1
2 address2
data :
id datatypeid datavalue mytableid
1 1 george street 1
mytable:
mytableid datecreated
1 2012-02-17 10:06:02.507
Can you help please? 你能帮忙吗?
spitballing this, but give it a try 恶作剧,但尝试一下
SELECT m.mytableid, CASE WHEN dt.datatypeid = 1 AND d.datatypeid =1 THEN d.datavalue ELSE null END AS "address1"
, CASE WHEN dt.datatypeid = 2 AND d.datatypeid =2 THEN d.datavalue ELSE null END AS "address2"
,m.datecreated
FROM datatype dt inner join
data d on dt.datatypeid = d.datatypeid inner join
mytable m on d.mytableid = m.mytableid
I don't think that this structure is optimal for what you want, but if you are stuck with it.... 我认为此结构并不是您想要的最佳结构,但是如果您坚持使用它...
something like this might help.(i have not tested it at all) 这样的事情可能会有所帮助。(我根本没有测试过)
My assumptions 我的假设
datatype table has more values than address1 and address2 数据类型表的值比地址1和地址2更多
WITH address1 as ( SELECT id,datavalue FROM data WHERE datatypeid = <datatypeid for addr1> ), address2 as( SELECT id,datavalue FROM data WHERE datatypeid = <datatypeid for addr2> ) SELECT d.id, a1.datavalue as address1, a2.datavalue as address2 FROM data d LEFT OUTER JOIN address1 a1 ON d.id = a1.id LEFT OUTER JOIN address2 a2 ON d.id = a2.id
again, you
might
will have to do some tweaking to the sql, because it would not perform in a production environment with a lot of rows 再次,您
可能
需要对sql进行一些调整,因为它不会在具有很多行的生产环境中执行
Don't understand well what's the structure of your database but I can help with my suggestion: 不太了解您的数据库的结构,但是我可以提供建议:
SELECT B.id
FROM datatype A INNER JOIN
data B ON datatype.DataTypeID = data.DataTypeID INNER JOIN
mytable C ON data.ID = mytable.ID
This helps to be sure that there's no conflict between attributes. 这有助于确保属性之间没有冲突。
RQ: You have put data.ID
with ID (uppercase) in INNER JOIN
and in SELECT
you have put in lowercase. RQ:您已将ID为ID(大写)的
data.ID
放在INNER JOIN
而在SELECT
您将小写。 This may cause the problem, I'm not sure. 我不确定这可能会导致问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.