Home > Software engineering >  Does Cross Join not work between two different tables with same column name?
Does Cross Join not work between two different tables with same column name?

Time:10-03

As written on the title, does CROSS JOIN not work for different tables with the same column name?

For example, I have one table named Fruits:

|  name  |  price  |
|  apple |    1    |
| banana |    2    |

and another table named Snacks:

|  name   |  price  |
|  chips  |    3    |
| cookies |    4    |

Then does

SELECT Fruits.price, Snacks.price FROM Fruits CROSS JOIN Snacks

does not work properly? I am working on a same issue, but the result shows like:

|  price  |  price  |
|    3    |    3    |
|    4    |    4    |
|    3    |    3    |
|    4    |    4    |

But what I expect is:

|  price  |  price  |
|    1    |    3    |
|    1    |    4    |
|    2    |    3    |
|    2    |    4    |

CodePudding user response:

As I mentioned in the comment, it is not possible. Either your tables values are different or your query.

Check this dbfiddle showing the result value same as your expected values.

CodePudding user response:

In MySQL CROSS JOIN works as expected:

 price  price 
 ------ ----- 
 1      3     
 2      3     
 1      4     
 2      4     

See running example at DB Fiddle.

  • Related