PHP-PDO-MySQL-Class icon indicating copy to clipboard operation
PHP-PDO-MySQL-Class copied to clipboard

How would we use UNION or JOINS in SELECT queries?

Open seojake opened this issue 5 years ago • 3 comments

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

seojake avatar Mar 20 '19 17:03 seojake

$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)
) );

lincanbin avatar Mar 20 '19 17:03 lincanbin

This is still giving me the same error.

seojake avatar Mar 21 '19 10:03 seojake

Hi @seojake, try using query builder https://github.com/nilportugues/php-sql-query-builder, this was helpful for a friend of mine

marcus-hiles avatar Mar 27 '19 10:03 marcus-hiles