如何从多个表中获取数据?

时间:2022-06-25 16:58:51

I am using PHP. I have following tables

我正在使用PHP。我有以下表格

1)Posts

||  post_id  |  post  |  date_posted  ||


2) Photo

||  photo_id  |  photo  |  date_posted||


3) Video

||  video_id  |  video  |  video_link  |  date_posted||

I want to retrieve all rows present in all tables and order by date_posted.

我想检索所有表中的所有行,并按date_posted排序。

I want to differentiate later after retrieving rows which one is video and which one is post

我想在检索一行是视频和哪一个是发布的行后稍后进行区分

How can i do this?

我怎样才能做到这一点?

2 个解决方案

#1


0  

SELECT post_id as id, post as data, date_posted FROM Posts
UNION ALL
SELECT photo_id, photo, date_posted FROM Photo
UNION ALL
SELECT video_id, video, date_posted FROM Video
ORDER BY date_posted DESC

maybe?..

#2


1  

 (
   SELECT video_id   AS id,
          'Post'     AS content_type,
          post       AS content,
          date_posted
     FROM Posts

        UNION ALL

   SELECT photo_id   AS id,
          'Photo'    AS content_type,
          photo      AS content,
          date_posted
     FROM Photo

           UNION ALL

   SELECT video_id   AS id,
          'Video'    AS content_type,
          video      AS content,
          date_posted
     FROM Video
 )
 ORDER BY date_posted DESC

#1


0  

SELECT post_id as id, post as data, date_posted FROM Posts
UNION ALL
SELECT photo_id, photo, date_posted FROM Photo
UNION ALL
SELECT video_id, video, date_posted FROM Video
ORDER BY date_posted DESC

maybe?..

#2


1  

 (
   SELECT video_id   AS id,
          'Post'     AS content_type,
          post       AS content,
          date_posted
     FROM Posts

        UNION ALL

   SELECT photo_id   AS id,
          'Photo'    AS content_type,
          photo      AS content,
          date_posted
     FROM Photo

           UNION ALL

   SELECT video_id   AS id,
          'Video'    AS content_type,
          video      AS content,
          date_posted
     FROM Video
 )
 ORDER BY date_posted DESC