Join 3 Distinct Tables in MySQL with no Common or Linked Column
I Have Three Distinct Tables in Mysql which have no column in common, I want to access one column from each table & show all of them combined elsewhere.
Table Details are
There are many more columns in each table but i have mentioned what i want to access.I Have tried the following code but this returns only column names as output & no column data is received in output.I am a beginner in Databases so please help me with this.
SELECT * FROM screen
JOIN movie ON movie.M_Code = screen.S_Id
JOIN showdetails ON showdetails.Show_Id = screen.S_Id;
you can create 3 individual select statements and link them on a hard coded artificial key
select a, b, c, d, e, f from (
(select a, b, c, 1 as key from atable) aa
inner join
(select d, e, f, 1 as key from btabke) bb
on aa.key = bb.key
something along those lines
essentially, each select statement becomes its own table, selecting only those rows which you want to be linked to the other select based tables. The artificial key is a simple way of linking them together since you have no other means of doing so
I am not exactly sure I understand what you want to achieve. Are those tables somehow related to each other, eg has every movie a screen on which it is shown on? If so, you had to join like this:
SELECT * FROM screen JOIN movie ON movie.S_Id = screen.S_Id
Imagine your tables are like the following:
Screen
S_Id| name ----------- 1 | Main Screen 2 | Screen 2 3 | Little Screen
Table
M_Code| Name ------------- 1 | Find Nemo 2 | Odyssey in Space 3 | Lord of the Rings
Your join statement would join the IDs as they are, in this case Find Nemo to Main Screen, Odyssey in Space to Screen 2 and LOTR to the Little Screen. Thats probably not what you wanted to achieve, so you have to use or add a column which links those data rows to each other.
If you instead want to get every possible combination, you can use Constantins answer, but I think if you have many datasets this could get very slow. Instead you could retrieve all datasets of every table and combine only those which make sense in your program.
链接地址: http://www.djcxy.com/p/72722.html