2016-05-12 5 views
-1

ich zwei select-Anweisungen mit unterschiedlichen Ergebnismengen machen, Bitte helfen Sie uns diese beide select-Anweisungen zeigen in einer Tabelle, die die ihr Ergebnis zu machen,SQL Server, wie zwei in einer select-Anweisung Tabelle erstellen

DECLARE @EmpID INT 

SELECT @EmpID = SubCategoryId 
FROM dbo.Product 
WHERE ProductId = 13 

SELECT Product.ProductId 
    , Product.ProductName 
    , Product.ProductPrice 
    , Product.ProductQuantity 
    , Product.SubCategoryId AS ForUpdate 
    , SubCategory.SubCategoryName 
    , SubCategory.SubCategoryId 
FROM Product 
INNER JOIN ProductUnderCategory 
    ON ProductUnderCategory.ProductId = Product.ProductId 
INNER JOIN SubCategory 
    ON ProductUnderCategory.SubCategoryId = SubCategory.SubCategoryId 
WHERE Product.ProductId = 13 

SELECT Property.Propertyid 
    , Property.PropertyName 
    , ProductProperties.PropertyValue 
FROM Property 
LEFT JOIN ProductProperties 
    ON Property.PropertyId = ProductProperties.PropertyId AND ProductProperties.ProductId = 13 
WHERE Property.Propertyid IN (
     SELECT PropertyId 
     FROM CategoryProperty 
     WHERE CategoryProperty.SubCategoryId = CategoryProperty.SubCategoryId AND CategoryProperty.SubCategoryId = @EmpID 
     ) 
+2

Verwenden Sie mySQL oder MS SQL Server? Sie haben beide Tags. – Morpheus

+0

Möchten Sie alle Zeilen aus beiden Ergebnismengen kombinieren? Wenn dies der Fall ist, müssten die Spalten "aufgereiht" werden und Sie haben nicht die gleiche Anzahl von Spalten zurückgegeben. – Morpheus

+0

Wenn dies SQL Server ist (ich weiß nichts über mysql), dann brauchen Sie eine UNION, aber wie Morpheus sagte, müssen die Spalten der beiden Abfragen übereinstimmen. – OldBoyCoder

Antwort

0

Ich gehe davon aus Sie die Eigenschaften für jedes Produkt wünschen (kein Verein, sondern eine Verknüpfung). Etwas wie dieses:

DECLARE @EmpID INT 

SELECT @EmpID = SubCategoryId 
FROM dbo.Product 
WHERE ProductId = 13 

SELECT Product.ProductId 
    , Product.ProductName 
    , Product.ProductPrice 
    , Product.ProductQuantity 
    , Product.SubCategoryId AS ForUpdate 
    , SubCategory.SubCategoryName 
    , SubCategory.SubCategoryId 
    , Property.Propertyid 
    , Property.PropertyName 
    , ProductProperties.PropertyValue 
FROM Product 
INNER JOIN ProductUnderCategory 
    ON ProductUnderCategory.ProductId = Product.ProductId 
INNER JOIN SubCategory 
    ON ProductUnderCategory.SubCategoryId = SubCategory.SubCategoryId 
LEFT JOIN ProductProperties 
    on ProductProperties.ProductID = Product.ProductID 
left join Property 
    ON Property.PropertyId = ProductProperties.PropertyId 
    and Property.Propertyid IN (
     SELECT PropertyId 
      FROM CategoryProperty 
      WHERE 
       CategoryProperty.SubCategoryId = CategoryProperty.SubCategoryId 
       AND CategoryProperty.SubCategoryId = @EmpID 
      ) 
WHERE Product.ProductId = 13 
+0

Es funktioniert, vielen Dank Chris Steele, Gott segne Sie! – Lucy

0

Sie können wahrscheinlich Folgendes tun: Wenn in der ersten Auswahl keine Spalte vorhanden ist, müssen Sie den entsprechenden Wert angeben und die Spalte benennen. Wenn in den folgenden Auswahlfeldern keine Spalte vorhanden ist, geben Sie einfach null/''/0 oder was immer Sie benötigen, als Wert zurück;

select Product.ProductId, Product.ProductName, Product.ProductPrice, Product.ProductQuantity, '' as someColumn, .... 
from .... 
union [all] 
Select Property.Propertyid, Property.PropertyName, ProductProperties.PropertyValue, NULL (for ProductQuantity), ProductProperties.someColumn 
from ..... 
Verwandte Themen