-1

I have three tables, and I want to fetch the three table data without a foreign key.

Say I have a table product_1 which contains (id, name) and table product_2 contain (id, name, price).

I want to select the columns of the two tables in one query.

I am asking if there are any MySQL query to select many columns from many tables?

This is my code:

SELECT name FROM product1 AND price FROM product2;
Vérace
  • 30,923
  • 9
  • 73
  • 85

2 Answers2

0

I have three tables, and I want to fetch the three table data without a foreign key.

I think you mean "without a join condition".
That's a Bad Idea.

If you don't tell the database how to join two tables, it will perform a Cartesian Join, i.e. it will match every row in the first table against every row in the second table.

  • If you have ten rows in both tables, you will get back a resultset of one hundred (10 x 10) rows.
  • If you have a million rows in both tables, you will get back a resultset of one trillion (10^6 x 10^6) rows - assuming your database survives long enough to deliver it and you're prepared to wait that long!

And the more tables you add, the more this multiplies out!

Phill W.
  • 9,889
  • 1
  • 12
  • 24
0
SELECT a.id, a.name, p.price
    FROM Names  AS a
    JOIN Prices AS p  ON p.id = a.id AND p.name = a.name;

This works with or without indexes, with or without FOREIGN KEYs. But performance is better with.

Rick James
  • 80,479
  • 5
  • 52
  • 119