Select fields from different tables in SQL without a need of matching fields [closed]
This is just a random question my friend asked me which I also do not know, yet I wanna know :/
Say I want to get this kind of result:
mytable_id bar
1 content2
3 content1
from the tables below:
mytable
mytable_id foo
1 something1
2 something2
3 something3
yourtable
yourtable_id bar
200开发者_JAVA百科1 content1
2002 content2
2005 content3
Many thanks for the help :)
SELECT mytable.mytable_id, yourtable.bar FROM mytable, yourtable ORDER BY RAND() LIMIT 2
Well, as there is no relation between the two tables, you have to make a cartesian product, order the rows randomly and limit the output to the number of rows you want.
I think you're going to struggle with that as it breaks the rules by not having corresponding column names.
You could try to use INNER JOIN and then select the two columns, although you'll receive duplicate entries.
Using UNION would collate the information into one column.
I think a better question would be why would you want to do that in the first place, and then design the database accordingly.
精彩评论