Codeigniter combine two results - php

I have two queries. I have to execute both queries and return in same result object.
for example
$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 ;
I have to return the result of both queries in same object. please help me
the actual query is ::
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

You can use UNION
$query = "SELECT name,age from students
UNION
SELECT name,age from users";
$result = $this->db->query($query);
Or if you want to identify records from which table row belongs to then
$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);

You might want to search for a question before asking it..
Here is the same question already with an answer.

$query = "SELECT name,age from students UNION ALL SELECT name,age from users";
You need to use UNION ALL instead of UNION in your query
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 removes repeating rows in final result set while UNION ALL keeps the duplicate.

Related

How to save and print selected result from multiple mysql tables in one array?

I need to get post_id count from six table but i can not do it with using UNION in one query
A query is prepared successfuly but when i need to display result it's not working
could someone explain what's wrong and how to solve problem?
$query = "(SELECT COUNT(post_id) as realestate_count FROM realestate)
UNION
(SELECT COUNT(post_id) as cars_count FROM cars)
UNION
(SELECT COUNT(post_id) as spectechnic_count FROM spectechnic)
UNION
(SELECT COUNT(post_id) as motorcycles_count FROM motorcycles)
UNION
(SELECT COUNT(post_id) as parts_count FROM parts)
UNION
(SELECT COUNT(post_id) as beauty_count FROM beauty)";
$stmt = $conn->prepare($query);
$stmt->execute();
$result = $stmt->get_result();
$result = $result->fetch_assoc();
print_r($result["realestate_count"]); // got undefined array key error with all
print_r($result["cars_count"]);
print_r($result["spectechnic_count"]);
print_r($result["motorcycles_count"]);
print_r($result["parts_count"]);
print_r($result["beauty_count"]);
Please use subqueries instead of union to get what you want.
SELECT COUNT(post_id) as realestate_count ,
(SELECT COUNT(post_id) FROM cars) as cars_count,
(SELECT COUNT(post_id) FROM spectechnic)as spectechnic_count ,
(SELECT COUNT(post_id) FROM motorcycles)as motorcycles_count,
(SELECT COUNT(post_id) FROM parts) as parts_count,
(SELECT COUNT(post_id) FROM beauty) as beauty_count
FROM realestate

mysql inner join query where id is obtained from $_GET['id']

I have two tables firm and contactdetails. I am trying to get the the firm name from firm and certain contact details from contactdetails. I am using $id =$_GET['id']; to get the id . In contactdetails i have fk_firm_id which is my foreign key. I am not sure how to use the inner join query. I am trying the following query:
$sql="SELECT firm.`name` ,`address_physical_line_1` , `fax_1` , `phone_1`
FROM contactdetails JOIN firm ON contactdetails.fk_firm_id='$id'";
echo $sql;
$result = mysql_query($sql);
but i am not getting the correct firm. Can anyone help me with this query, please.
You should use like this JOIN firm ON contactdetails.fk_firm_id = firm.id
$sql=" SELECT firm.`name` ,`address_physical_line_1` , `fax_1` , `phone_1`
FROM contactdetails
JOIN firm ON contactdetails.fk_firm_id = firm.id
WHERE contactdetails.fk_firm_id = '$id'
";
$result = mysql_query($sql);
This is assuming that your firm table has a primary key called id
$sql="SELECT firm.`name` ,`address_physical_line_1` , `fax_1` , `phone_1` FROM contactdetails JOIN firm ON `contactdetails`.`fk_firm_id`=`firm`.`id`
WHERE `firm`.`id` = '$id'";
echo $sql;
$result = mysql_query($sql);
There is a mistake about JOIN and WHERE statements:
$sql = "SELECT
f.name,
c.address_physical_line_1,
c.fax_1,
c.phone_1
FROM
contactdetails c JOIN firm f ON c.fk_firm_id= f.id
WHERE c.id = '$id'";
$sql="SELECT firm.`name` ,`address_physical_line_1` , `fax_1` , `phone_1` FROM
contactdetails JOIN firm ON contactdetails.fk_firm_id=firm.id where
contactdetails.fk_firm_id='$id'";
you should join on a firm's field such as firm.id
syntax: FROM table1 LEFT JOIN table2 ON table1.field1 compopr
table2.field2 compopr is : "=","<",">","<=",">=","<>"
You are missing the WHERE clause that limits the result set to only the firm you're interested in; now you're getting all firms joined with a single contact details record.
.. where firm.id=$id
For new applications, please use a database API that has prepared statements, like mysqli or pdo.
Use the following query for inner join
$sql="SELECT firm.name ,address_physical_line_1 , fax_1 , phone_1 FROM
contactctdetails INNER JOIN firm ON contactdetails.fk_firm_id=$id";

mysql_num_rows from multiple tables

I'm looking to count the amount of fields in 5 tables and display a result. I currently do this for a single table
$variable = mysql_num_rows(mysql_query("SELECT id FROM table1"));
What is the most efficient way to include and count all id from table2, table3, table4 and table5?
Thanks
Use UNION ALL to combine the SELECT queries on all the tables to get the total number of IDs from all the tables. Use UNION instead to get total number of distinct IDs from all the tables.
$variable = mysql_num_rows(mysql_query("
"SELECT id FROM table1 " .
"UNION ALL " .
"SELECT id FROM table2 " .
"UNION ALL " .
"SELECT id FROM table3 " .
"UNION ALL " .
"SELECT id FROM table4 " .
"UNION ALL " .
"SELECT id FROM table5"));
Don't get all rows from your DB, just get the num of id's. Let MySQL count instead of PHP.
$variable1 = mysql_query("SELECT COUNT(id) FROM table1");
$variable2 = mysql_query("SELECT COUNT(id) FROM table2");
$variable3 = mysql_query("SELECT COUNT(id) FROM table3");
$variable4 = mysql_query("SELECT COUNT(id) FROM table4");
$variable5 = mysql_query("SELECT COUNT(id) FROM table5");
$variable = $variable1 + $variable2 + $variable3 + $variable4 +$variable5;
Don't make UNION or JOIN neither, it's a heavywight job if you just need the count.
SELECT count(t1.id)+count(t2.id)+count(t3.id)+count(t4.id)+count(t5.id) from table1 as t1, table2 as t2, table3 as t3, table4 as t4, table5 as t5
SELECT COUNT(id) FROM table2
etc.
try union syntax like this:
$variable = mysql_num_rows(mysql_query("
SELECT * FROM(
SELECT id FROM table1
UNION
SELECT id FROM table2
UNION
SELECT id FROM table3
UNION
SELECT id FROM table4
UNION
SELECT id FROM table5
)unionNum
"));
Finish, good luck
First Method:
$query = mysql_query("
select * from(
SELECT id FROM table1
union
SELECT id FROM table2
union
SELECT id FROM table3
union
SELECT id FROM table4
)
");
while ($row = mysql_fetch_assoc($query)) {
//some operations
}
$variable = mysql_num_rows($query);
Second Method:
$query = mysql_query("
select count(*) cnt from(
SELECT id FROM table1
union
SELECT id FROM table2
union
SELECT id FROM table3
union
SELECT id FROM table4
)
");
while ($row = mysql_fetch_assoc($query)) {
$variable = $query['cnt'];
}

Nested queries syntax improvement

What's the correct syntax to achieve this using 1 query instead of 2?
$result = mysql_query(SELECT * FROM users);
while($row = mysql_fetch_array($result)){
$result = mysql_query(SELECT SUM(balance) FROM users_account WHERE uid=$row[id]);
}
You should inner join both tables and group by user table.
You can add more columns in SELECT clause, remember to add also same columns to GROUP BY clause to get a standard SQL statement.
$query = " SELECT u.uid, SUM(a.balance)
FROM users_account a
INNER JOIN users u
ON u.uid = a.uid
GROUP BY u.uid";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
...
Arrange $query concatenating strings if needed.
This will return all records form user_account table with user info :-
SELECT SUM(ua.balance),u.*,ua.* FROM users_account ua
left join users u on ua.users_account=u.id
GROUP BY u.id
$result = mysql_query(SELECT a.*, b.SUM(balance) AS user_balance FROM users a, users_account b WHERE uid=$row[id]);

php mysql join query

I need to pull data from 2 tables in my database. The data I pull from table 2 depends on the result of table 1.
I'm not amazing at all these JOINS and things, so if someone could just explain what kind of a JOIN i'd need here, and how it would look, i'd be grateful:
$sql_result = mysql_query("SELECT * FROM accounts WHERE id='$val'", $db);
$rs = mysql_fetch_array($sql_result); $name = $rs[name];
$sql_result2 = mysql_query("SELECT * FROM players WHERE name='$name'", $db);
$rs2 = mysql_fetch_array($sql_result2);
$sql="SELECT * FROM accounts JOIN players ON accounts.accounts_link_to_player_id_here=players.id WHERE accounts.id='$val'";
You can do something like this, depending on the structure of the table(s):
SELECT * FROM `accounts` INNER JOIN `players` USING (`name`) WHERE `accounts`.`id` = 'value';
SELECT * FROM accounts LEFT JOIN players USING (name) WHERE accounts.id = 'value';
You'll need a query that looks like this (this is known in SQL as a subselect):
"SELECT p.* FROM players p WHERE name IN ( SELECT n.name FROM accounts n WHERE n.id = '$val' )"

Categories