简体   繁体   中英

Mysql join 2 tables that are unrelated to each other

Is there a way to join 2 tables that are completly unrelated to each other?

I have 2 tables, named 'writes' and 'posts' The posts are messages that people wrote on their own profile The writes are messages that people wrote on another users profile.

While loading a users profile, i want a row of al their posts and writes on their timeline. How can i achive this?

Is there a way to join these tables together? Or should i make an complete other system to load the timeline?

Things like

SELECT q1, q2 FROM
(SELECT posts.* as q1 FROM writes as a) t1, 
(SELECT writes.* as q2 FROM posts as b) t2

didn't work for me

And select * from posts, writes gives me an cross join table.

The tables has the following structure:

posts:

#1 id int
#2 user_id int
#3 post varchar (4096)
#4 last_changed datetime
#5 posted_at datetame
#6 type varchar (32) (In the post case it is 'POST')

write:

#1 id int
#2 writer_id int (the user_iud who has written it)
#3 user_id int (the user_id of the person this write is written on)
#4 written varchar (the text)
#5 last_changed datetime
#6 written_at datetime
#7 type varchar (32) (in this case it it 'WRITE')

You can combine results of Different Select queries into one using UNION . Since there will not be any duplicates between the two Select statements, you can make it more efficient using UNION ALL

Also, to do overall sorting , you need to take the complete result-set in a Derived table , and use an Order By clause. You can set it in Ascending or Descending order, using ASC or DESC respectively.

We also need to ensure that number of columns returned by both the SELECT statements are same:

SELECT dt.id, 
       dt.writer_id, 
       dt.user_id, 
       dt.post, 
       dt.last_changed, 
       dt.posted_at, 
       dt.type 
FROM 
(
 (
  SELECT id, 
         user_id AS writer_id, 
         user_id, 
         post, 
         last_changed, 
         posted_at, 
         type
  FROM posts
 )

  UNION ALL 

 (
  SELECT id, 
         writer_id, 
         user_id, 
         written AS post, 
         last_changed, 
         written_at AS posted_at, 
         type
  FROM writes
 )
) AS dt 
ORDER BY dt.posted_at ASC 

Documentation details:

UNION is used to combine the result from multiple SELECT statements into a single result set.

The column names from the first SELECT statement are used as the column names for the results returned. Selected columns listed in corresponding positions of each SELECT statement should have the same data type. (For example, the first column selected by the first statement should have the same type as the first column selected by the other statements.)

If the data types of corresponding SELECT columns do not match, the types and lengths of the columns in the UNION result take into account the values retrieved by all of the SELECT statements.

use union all

select  id ,user_id,post,last_changed,posted_at,type from posts
 union all    
select  id,user_id,written,last_changed,written_at,type from write

Rule of union all

Each SELECT statement within UNION must have the same number of columns
The columns must also have similar data types
The columns in each SELECT statement must also be in the same order

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM