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.
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.
I have tried using UNION ALL
to no avail.
$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. If I run this code in MySQLWorkbench
it say's
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...
(Note: I'm pretty new to the whole SQL thing, so please be gentle ...)
Again thanks for your time...