[英]Combining two SQL queries PHP
Thanks for reading my question. 感谢您阅读我的问题。 I've been going at this for a day and a half now and I cant seem to get this to work.
我已经花了一天半的时间了,我似乎无法使它正常工作。 What I'm trying to do is combine two SQL queries.
我想做的是结合两个SQL查询。
The connection to the database is correctly set-up. 与数据库的连接已正确建立。 This is all working.
一切正常。 So the queries I'm trying to combine are:
因此,我要合并的查询是:
$sql = "SELECT * FROM pf_postmeta WHERE meta_key = '_wp_attached_file' ";
$result = $conn->query($sql);
foreach ($result as $row) {
// do my stuff
}
$conn->close();
And 和
$sql = "SELECT * FROM pf_posts WHERE post_type = 'cases' ";
$result = $conn->query($sql);
foreach ($result as $row) {
// do my stuff
}
$conn->close();
Now I've read about JOIN
and UNION
but I can't get the two queries to work together. 现在,我已经阅读了有关
JOIN
和UNION
但是我无法使这两个查询一起工作。
I have tried using UNION ALL
to no avail. 我尝试使用
UNION ALL
无济于事。
$sql = "SELECT *FROM pf_postmeta WHERE meta_key = '_wp_attached_file'
UNION ALL
SELECT *FROM pf_posts WHERE post_type = 'cases'";
$result = $conn->query($sql);
foreach ($result as $row) {
// do stuff
}
$conn->close();
This doesn't show anything. 这什么也没显示。
UPDATE AS REQUESTED: If I remove the UNION ALL SELECT
part my query works. 要求的更新:如果删除
UNION ALL SELECT
部分,则查询有效。 If I run this code in MySQLWorkbench
it say's 如果我在
MySQLWorkbench
运行此代码,则表示
09:07:30 SELECT * FROM deb100651n2_pf.pf_posts WHERE post_type='cases' UNION ALL SELECT * FROM deb100651n2_pf.pf_postmeta WHERE meta_key = '_wp_attached_file' Error Code: 1222. The used SELECT statements have a different number of columns 0.050 sec
Is there anyone who has a solution form me? 有谁可以解决我的问题? This has been bugging me for the past 1,5 day's...
在过去的1.5天里,这一直困扰着我。
(Note: I'm pretty new to the whole SQL thing, so please be gentle ...) (注意:我对整个SQL还是很陌生的,所以请谨慎点...)
Again thanks for your time... 再次感谢您的时间...
To perform a "union query" the number of columns MUST match : plus , the data type of each column must be compatible (eg column 3 of first query is decimal, and column 3 of second query is integer). 要执行“联合查询” ,列数必须匹配 : plus , 每列的数据类型必须兼容 (例如,第一个查询的第3列为十进制,而第二个查询的第3列为整数)。 If either the number of columns is different, or any column is of an incompatible type, the query will produce an SQL error.
如果列数不同,或者任何列的类型不兼容,则查询将产生SQL错误。
So. 所以。 Here is a very great reason to cease using
select *
in any production code. 这是在任何生产代码中停止使用
select *
重要原因。 You must specify the columns names, making sure both select lists have the same number of columns, and that those columns are compatible. 您必须指定列名称,并确保两个选择列表的列数均相同,并且这些列兼容。
As far I guessed from what you have stated above there might be column miss match issue in your union query. 据我从您上面所说的猜测来看,在联合查询中可能存在列未命中匹配的问题。 While using
union
operator you must remember three things- 使用
union
运算符时,您必须记住三件事:
SELECT
statement within UNION must have the same number of columns SELECT
语句必须具有相同的列数 SELECT
statement must also be in the same order SELECT
语句中的列也必须具有相同的顺序 Therefore if you perform a SELECT * ..
operation on both table then your pf_postmeta
and pf_post
table must have same number of column having similar data types and ordering. 因此,如果您在两个表上都执行
SELECT * ..
操作,则您的pf_postmeta
和pf_post
表必须具有相同数据类型和顺序的相同列数。 You may try SELECT id from pf_postmeta UNION ALL SELECT id from pf_post
for clearing your concept how union works assuming there is id
column at both table. 您可以尝试
SELECT id from pf_postmeta UNION ALL SELECT id from pf_post
以清除您的概念(假设两个表中都有id
列)。
why to make so complicate make it simple. 为什么如此复杂使它变得简单。 Try it:
试试吧:
SELECT * FROM pf_postmeta
WHERE meta_key = '_wp_attached_file'
UNION
SELECT * FROM pf_posts
WHERE post_type = 'cases'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.