开发者

Select fields from different tables in SQL without a need of matching fields [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜