0

I am trying to create a SQL view which contains columns from different tables; the columns are different data types.

For example;

I have table a with a column that contains usernames. The data type of this column is nvarchar.

I then have table b, which has a column that contains whether a document was printed in colour or not – the data is either yes or no. The data type of this column is bit.

I want the view to show both the above columns side by side, so I can then pull the information into Excel for reporting purposes.

I am pretty new to SQL so I am learning as I go along.

4

1 回答 1

0

就像 PM77-1 所说的那样,您必须有一些方法将两张桌子绑在一起。例如,如果您的表 b 还具有打印文档的人的用户 ID,则您的表将如下所示:

Table A                                    Table B
----------------------------               -----------------------------------
userID        userName                     docID    docName     inColor userID
----------------------------               -----------------------------------
1             userName1                    1        docName1    1       1
2             userName2                    2        docName2    0       2
3             userName3                    1        docName1    1       2
                                           3        docName3    0       1
                                           3        docName3    1       2
                                           2        docName2    1       3

您的查询可能如下所示:

SELECT a.userName, b.docName, b.inColor FROM a INNER JOIN b ON a.userID = b.userID ORDER BY a.userName, b.inColor;
于 2013-03-08T16:53:10.977 回答