PDO中的Union Select不起作用 [英] Union Select in PDO not working

查看:72
本文介绍了PDO中的Union Select不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是刚开始使用PDO发出数据库请求的人,需要一点帮助. 我有以下数据库调用:

I'm new to using PDO to make db requests and need a little help. I have the following db call:

$stmt1 = $pdo->prepare('
    SELECT * FROM news WHERE pub_date >= ? AND pub_date < ? AND display = 1 ORDER BY pub_date DESC
    UNION
    SELECT * FROM vs_news WHERE pub_date >= ? AND pub_date < ? AND display = 1 ORDER BY pub_date DESC
');
$stmt1->bindParam(1, $col_start);
$stmt1->bindParam(2, $col_end);
$stmt1->execute();

我已经阅读了足够多的文章,认为UNION与PDO兼容,但是我似乎无法正确编写代码,也无法找到完整代码格式的示例.

I have read enough to think the UNION is compatable with PDO, but I can't seem to get the code right and can't find an example in complete code format.

两个表中的字段相同,并且db调用仅适用于一个或另一个表,但不适用于我所显示的UNIION.

The fields in both tables are the same and the db call works with just one or the other table, but not with the UNIION that I have shown.

有人可以指出我的问题在哪里吗?

Could someone please point where my problem is?

谢谢

推荐答案

使用?意味着您需要为每个参数匹配一个参数?

Using the ? means you need to match a param for each ?

使用这种方法:

$stmt1 = $pdo->prepare('
    SELECT * FROM news WHERE pub_date >= :date1 AND pub_date < :date2 AND display = 1 ORDER BY pub_date DESC
    UNION
    SELECT * FROM vs_news WHERE pub_date >= :date1 AND pub_date < :date2 AND display = 1 ORDER BY pub_date DESC
');
$stmt1->bindParam(':date1', $col_start);
$stmt1->bindParam(':date2', $col_end);
$stmt1->execute();

也;通过并集,请确保在两个查询中使用相同的列数.

Also; With union, make sure that you use the SAME number of columns in both queries.

这篇关于PDO中的Union Select不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆