PHP-PDO-MySQL-Class
PHP-PDO-MySQL-Class copied to clipboard
How would we use UNION or JOINS in SELECT queries?
I'm looking to run a union query, here's what I've got:
$get_posts = $DB->query( "SELECT * FROM op_posts WHERE post_author_id IN (:ids) UNION SELECT * FROM op_post_shares WHERE share_user_id IN (:ids)", array('ids' => array(1, 2, 54)) );
But it spits out the error:
Cardinality violation: 1222 The used SELECT statements have a different number of columns
$get_posts = $DB->query( "SELECT * FROM op_posts WHERE post_author_id IN (:ids1) UNION SELECT * FROM op_post_shares WHERE share_user_id IN (:ids2)", array(
'ids1' => array(1, 2, 54),
'ids2' => array(1, 2, 54)
) );
This is still giving me the same error.
Hi @seojake, try using query builder https://github.com/nilportugues/php-sql-query-builder, this was helpful for a friend of mine