Home > Enterprise >  Join two simple SQL queries
Join two simple SQL queries

Time:03-07

I have two tables, ORDER and CART.

The following query returns all OrderId's associated with the customer email

SELECT [OrderId], [Email]
FROM [ORDER]
WHERE [Email] = '[email protected]'

Now I manually copy pasting OrderId to following quarries to return all CartId's associate with those OrderId's

SELECT TOP (1000) [CartId], [OrderId], [Name]
FROM [CART]
WHERE [OrderId] = 123
   OR [OrderId] = 456
   OR [OrderId] = 789

How can I join this two SQL queries to ease my work? I'm just a beginner.

CodePudding user response:

You may try the following join:

SELECT TOP 1000 c.CartId, c.OrderId, c.Name
FROM [CART] c
INNER JOIN [ORDER] o
    ON o.OrderId = c.OrderId
WHERE o.Email = '[email protected]';

CodePudding user response:

Use join as follows

SELECT [cartid],
       C.[orderid],
       [name],
       [email]
FROM   [cart] C
       JOIN [order] O
         ON O.[orderid] = C.[orderid]
WHERE  o.[email] = '[email protected]'
  • Related