Home > database >  Sql fetch result from multiple table having same column but no relation
Sql fetch result from multiple table having same column but no relation

Time:10-25

Hello all how can get two same name column from two different table as single result in two different column.

Eg: customer table have column customerid and order table has also column customerid want result from both table as separate independent record.

Result:

customerId | customerId

Note: There is no relation between both table

enter image description here

CodePudding user response:

If by result, you mean a simple SELECT query result, you can just indicate the column name in a SELECT clause and separate the table names with a comma in the FROM clause. see example below:

SELECT customerID FROM Customers, Orders;

You can also add a "WHERE" clause at the end if you have conditions the query needs to meet.

CodePudding user response:

I got a little confused, you want same column values or same values from two different columns?

same values from two tables you can use JOIN:

SELECT Customers.CustomerId, Orders.CustomerId
FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID=Orders.CustomerID;

for same columns in result, you can use simple select with comma (,) and using alias, a 'SELECT AS', to seprate column names:

SELECT o.CustomerID as OCustomerID, c.CustomerId as CCustomerID
FROM Customers AS c, Orders AS o;
  • Related