MySQL从一个从另一个表连接的表中选择所有左侧条目

时间:2017-04-06 07:39:44

标签: mysql inner-join

我有以下MySQL声明:

DECLARE @SearchStr nvarchar(100)
SET @SearchStr = '' 

--drop table #Results

CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))
SET NOCOUNT ON
DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110), @PrevColumnName nvarchar(128)
SET  @TableName = ''
SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''')

WHILE @TableName IS NOT NULL
BEGIN

SET @ColumnName = ''
SET @PrevColumnName = ''
SET @TableName = 'RAP1'

IF @TableName IS NOT NULL
BEGIN
    WHILE (@ColumnName IS NOT NULL)
    BEGIN

        SELECT TOP 1 @ColumnName = COLUMN_NAME
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_NAME    = PARSENAME(@TableName, 1)
            AND DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar', 'int', 'decimal')
            AND COLUMN_NAME > COALESCE(@PrevColumnName, @ColumnName)

        IF @ColumnName = @PrevColumnName
            begin
                SET @ColumnName = NULL
                SET @TableName = NULL
            end

        SET @PrevColumnName = @ColumnName

        IF @ColumnName IS NOT NULL
        BEGIN
            INSERT INTO #Results
            EXEC
            (
                'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630) FROM ' + @TableName + ' (NOLOCK) ' +
                ' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2
            )
        END
        END   
    END
END
SELECT ColumnName, ColumnValue FROM #Results

现在我得到的是表分配中的条目,其中包含该WireID的NormID和NormName。 我想得到的是表格规范中的条目,这些条目没有为此WireID设置。

例如为: WireID具有标准分配A,B,D,G。

表格范围包含条目A,B,C,D,E,F,G,H。

我想从MySQL-Statment获得的是条目C,E,F,H。

如何为此WireID选择左侧标准条目?

根据上述陈述,我会得到:

SELECT      norm.NormID, norm.NormName
FROM        (assignment
INNER JOIN  norm
ON          assignment.NID = norm.NormID )
INNER JOIN  wire
ON          assignment.LID = wire.WireID
WHERE       wire.WireID= 109 
ORDER BY    norm.NormName;  

我想要这张表:

-----------------------
| NormID  | NormName  |
-----------------------
| 1       |  A        |
| 2       |  B        |
| 4       |  D        |
| 7       |  G        |
-----------------------

3 个答案:

答案 0 :(得分:0)

我认为(如果我理解你的要求)你可以试试这个:

SELECT      norm.NormID, norm.NormName
FROM        assignment
INNER JOIN  norm ON assignment.NID = norm.NormID
LEFT JOIN   wire ON assignment.LID = wire.WireID
WHERE       assignment.LID= 109 
        AND wire.wireID IS NULL
ORDER BY    norm.NormName;  

在您发表评论后进行修改。 我想你可以使用:

SELECT      A.NormID, A.NormName
FROM        norm A
LEFT JOIN   (SELECT NID FROM assignment WHERE LID = 109) B ON B.NID = A.NormID
WHERE       B.NID IS NULL     
ORDER BY    A.NormName;  

OR

SELECT      A.NormID, A.NormName
FROM        norm A
WHERE NOT EXISTS (SELECT 1 FROM assignment WHERE LID = 109 AND ASSIGNMENT.NID = A.NormID)
ORDER BY    A.NormName;

答案 1 :(得分:0)

添加样本数据后,未使用109 wireid设置的条目为:

SELECT      norm.NormID, norm.NormName
FROM        assignment
inner JOIN  norm
ON          assignment.NID = norm.NormID 
INNER JOIN  wire
ON          assignment.LID = wire.WireID
WHERE       wire.WireID <> 109 
ORDER BY    norm.NormName;  

答案 2 :(得分:0)

试试这个:

[OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)]
public ActionResult Index()
{
    // code of Index()

}

我不太确定因为我没有你的数据