我有两个问题。我必须执行两个查询并返回相同的结果对象。 例如
$query1 = "SELECT name,age from students";
$query2 = "SELECT name,age from users";
$result1 = $this->db->query($query1)
$result2 = $this->db->query($query2)
return result1 and result 2 together ;
我必须在同一个对象中返回两个查询的结果。请帮帮我
实际查询是::
SELECT dev_members.name,dev_members.id,dev_members.age,dev_members.family_id,dev_family.house_name,dev_ib_account_registration.account_id FROM (dev_members)
JOIN dev_family ON dev_family.id=dev_members.family_id
JOIN dev_ib_account_registration ON dev_ib_account_registration.member_id=dev_members.id
UNION
SELECT dev_members.name,dev_members.id,dev_members.age,dev_members.family_id, dev_family.house_name,dev_ib_sub_member_registration.account_id FROM (dev_members)
JOIN dev_family ON dev_family.id=dev_members.family_id
JOIN dev_ib_sub_member_registration ON dev_ib_sub_member_registration.member_id=dev_members.id
答案 0 :(得分:1)
您可以使用UNION
$query = "SELECT name,age from students
UNION
SELECT name,age from users";
$result = $this->db->query($query);
或者,如果您想识别表行所属的记录,那么
$query = "SELECT name,age, 'students ' AS `table_type` from students
UNION
SELECT name,age, 'users' AS `table_type` from users";
$result = $this->db->query($query);
答案 1 :(得分:1)
您可能想在询问之前先搜索一个问题。
Here已经回答了同样的问题。
答案 2 :(得分:0)
$query = "SELECT name,age from students UNION ALL SELECT name,age from users";
您需要在查询中使用UNION ALL而不是UNION
SELECT dev_members.name,dev_members.id,dev_members.age,dev_members.family_id,dev_family.house_name,dev_ib_account_registration.account_id FROM (dev_members)
JOIN dev_family ON dev_family.id=dev_members.family_id
JOIN dev_ib_account_registration ON dev_ib_account_registration.member_id=dev_members.id
UNION ALL
SELECT dev_members.name,dev_members.id,dev_members.age,dev_members.family_id, dev_family.house_name,dev_ib_sub_member_registration.account_id FROM (dev_members)
JOIN dev_family ON dev_family.id=dev_members.family_id
JOIN dev_ib_sub_member_registration ON dev_ib_sub_member_registration.member_id=dev_members.id
当UNION ALL保留副本时,UNION会删除最终结果集中的重复行。