简体   繁体   English

当所有3个表都具有相同的列MYSQL时使用JOIN

[英]Using JOIN when all 3 tables have same columns MYSQL

I have 3 tables they are... 我有3张桌子...

  • posts_deletedPosts posts_deletedPosts
  • posts_archivedPosts posts_archivedPosts
  • posts_newPosts posts_newPosts

I am writing a function which allows users to see all of their posts and so I want to do a join query to pull all the data from these three tables back in one query. 我正在编写一个函数,该函数允许用户查看其所有帖子,因此我想进行联接查询,以将这三个表中的所有数据拉回到一个查询中。

All three tables has the exact same columns I want to get back, and they are... 这三个表都具有我想返回的完全相同的列,它们是...

  • title 标题
  • category 类别
  • dateCreated 创建日期
  • alias 别号

The query i've written so far I think should work, but when I go to use bind param I get this error. 到目前为止,我编写的查询我认为应该可以,但是当我使用绑定参数时,会出现此错误。

Warning: mysqli_stmt::bind_result() [mysqli-stmt.bind-result]: Number of bind variables doesn't match number of fields in prepared statement

My query looks like as follows... 我的查询如下所示...

  $stmt = $this->conx->prepare("SELECT 
    dp.title, dp.category, dp.dateCreated, dp.alias, 
    ap.title, ap.category, ap.dateCreated, ap.alias, 
    np.title, np.category, np.dateCreated, np.alias 
    FROM posts_deletedPosts AS dp 
        INNER JOIN posts_archivedPosts AS ap 
            ON dp.author = ap.author 
        INNER JOIN posts_newPosts AS np 
            ON dp.author = np.author 
    WHERE dp.author = ? 
    LIMIT 5");

I'm pretty new to web dev but I thought it would be possible to carry out a join like this and return one large dataset. 我对Web开发人员来说还很陌生,但是我认为可以进行这样的联接并返回一个大数据集。 I did use 3 select queries before but I want to build some pagination using AJAX and so I need this data in one complete set preferably. 我之前确实使用过3个选择查询,但是我想使用AJAX建立一些分页,所以我最好将这些数据放在一个完整的集合中。

This is how i bind the parameter 这就是我绑定参数的方式

$stmt->bind_param("i",$uid);

You don't need JOIN s, you need UNION s: 您不需要JOIN ,您需要UNION

SELECT title FROM posts_deletedPosts WHERE author=...
UNION
SELECT title FROM posts_archivedPosts WHERE author=...
UNION
SELECT title FROM posts_newPosts WHERE author=...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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