如何使用纯SQL实现PHP条件?

如何使用纯SQL实现PHP条件?,php,mysql,if-statement,Php,Mysql,If Statement,我有以下表格结构: // Posts +----+------------+-----------------------+----------------+-------------+ | id | title | content | money_amount | author_id | +----+------------+-----------------------+----------------+-------------+ | 1 |

我有以下表格结构:

// Posts
+----+------------+-----------------------+----------------+-------------+
| id |   title    |        content        |  money_amount  |  author_id  |
+----+------------+-----------------------+----------------+-------------+
| 1  | title 1    | content 1             | NULL           | 12345       |
| 2  | title 2    | content 2             | 25             | 42355       |
| 3  | title 3    | content 3             | 5              | 53462       |
| 4  | title 4    | content 4             | NULL           | 36346       |
| 5  | title 5    | content 5             | 15             | 13322       |
+----+------------+-----------------------+----------------+-------------+
//                                          ^^ NULL means this post is free


// Money
+---------+--------------+
| post_id | user_id_paid | 
+---------+--------------+
| 2       | 42355        |  // He is author of post
| 2       | 34632        |  // This row means besides author, this user 34632 can see this post too. Because he paid the money of this post.
| 3       | 53462        |  // He is author of post
| 5       | 13322        |  // He is author of post
| 3       | 73425        |  // This row means besides author, this user 34632 can see this post too. Because he paid the money of this post.
+---------|--------------+
注1:表中的所有
post\u id
(s)属于非免费的帖子

注2:
Money
表中始终有一行属于post作者(非免费)

注3:
Money
表只是用来确定谁可以看到这样的帖子


现在,此用户
$\u会话['current\u user']='23421'
想要查看此帖子
id=2
。这是我的密码:

$stm = $this->dbh->prepare(SELECT * FROM Posts WHERE id = '2');
$stm->execute();
$result = $stm->fetch(PDO::FETCH_ASSOC);

if ( $result[money] == '') {   // money_amount is NULL in the Posts table
    this post is free and everybody can see it
} else {

    $stm = $this->dbh->prepare(SELECT count(1) FROM Money WHERE post_id = '2' and user_id = $_SESSION['current_user']);
    $num_rows = $stm->fetchColumn();

    if($num_rows){
        $paid = true;  // This means current user paid the cost of post and he can see it.
    } else {
        $paid = false; // this means current user didn't pay the cost of post and he cannot see it.
    }
}

我想知道,我是否可以在一个查询中实现这两个查询,并使用MySQL而不是PHP实现该条件?

您可以使用联接,下面的查询使用
左联接

SELECT * FROM Money
LEFT JOIN Posts ON Money.post_id = Posts.id
WHERE ((Posts.money_amount IS NOT NULL AND Money.user_id_paid = :userId)
      OR Posts.money_amount IS NULL) AND Posts.id = :postId
请注意,
:userId
PDO
参数化查询的占位符,您应该在执行前将参数绑定到占位符。比如:

$postId = 2;
$stmt->bindParam('userId', $_SESSION['current_user']);
$stmt->bindParam('postId', $postId);

还要注意,绑定占位符名称时不需要冒号。使用
右键联接
意味着您从
Posts
表中选择
,并联接
Money

如果
存在
函数(MySql),下面是使用
的解决方案:


@frz3993实际上我需要检查
money\u amount
列的值是否为
NULL
然后
join
money
表并检查当前用户的id是否存在。我如何使用MySQL实现这个条件呢?试试这个查询,我写得很快,我不知道它是否正确。如果结果>0,用户可以看到帖子<代码>从货币、帖子(邮政ID=2和POSS.MouyyAUMT为空)或(Muny.PasySID=2和Muny.UsRyIdApLay==$Sale[ [ CurrutuxAudi] ])/if > IF语句中的子查询>中选择计数(*):
...
$stmt = $conn->prepare(" 
        SELECT IF(p.money_amount,1,0) as notfree, 
        EXISTS(SELECT * FROM `Money` WHERE `post_id` = ? AND`user_id_paid` = ?) as paid
        FROM `Posts` p WHERE p.id = ? ");

$stmt->execute([2, $_SESSION['current_user'], 2]);
$result = $stmt->fetch(\PDO::FETCH_ASSOC);

if (!$result['notfree']) {  // post is free
    // this post is free and everybody can see it
} else {
    $paid = ($result['paid'])? true : false; 
}