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'];
}
Related
Hi this is my query from this query i am getting 3 records i want only one record and i am trying to group by studentid
foreach ($gdFinaldata['gdskill'] as $skillId => $skillScore){
$filterQuery .= $union_all. "SELECT stu.student_pid
FROM tbl_students stu LEFT JOIN r_job_invitations jbi ON stu.student_email = jbi.email
LEFT JOIN r_job_scores jsc ON jsc.student_id = stu.student_pid
WHERE job_id = ".$jobID." AND skill_id = ".$skillId." AND gd_score >= ".$skillScore." ";
$union_all=" UNION ALL ";
} //for ends here
the recodrs will come like this:
SELECT stu.student_pid FROM tbl_students stu LEFT JOIN r_job_invitations jbi ON stu.student_email = jbi.email LEFT JOIN r_job_scores jsc ON jsc.student_id = stu.student_pid WHERE job_id = 88 AND skill_id = 3 AND gd_score >= 1
UNION ALL
SELECT stu.student_pid FROM tbl_students stu LEFT JOIN r_job_invitations jbi ON stu.student_email = jbi.email LEFT JOIN r_job_scores jsc ON jsc.student_id = stu.student_pid WHERE job_id = 88 AND skill_id = 63 AND gd_score >= 2
UNION ALL
SELECT stu.student_pid FROM tbl_students stu LEFT JOIN r_job_invitations jbi ON stu.student_email = jbi.email LEFT JOIN r_job_scores jsc ON jsc.student_id = stu.student_pid WHERE job_id = 88 AND skill_id = 128 AND gd_score >= 3
After this i want to add group by how can i do that i tried like this:
$groupby = "GROUP BY student_id";
$filterStudents = $conn->query($filterQuery.$groupby);
My issue is still returning 3 records i want to group by student
For Reference:
There are several ways to do this.
Use union instead of union all, this will retain only unique entries
select ...
union
select ...
union
select ...
If you want to keep union all (why do you?), use parenthesis around your selects and append group by after it
select student_pid
( select student_pid, student_id from ...
union all
select student_pid, student_id from ...
union all
select student_pid, student_id from ... ) as t
group by student_id
Your PHP would then look like
foreach (...) {
$filterQuery .= $union_all. "SELECT stu.student_pid, jsc.student_id ..."
// ...
}
$outerQuery = "select student_pid ("
$groupby = ") as t GROUP BY student_id";
$filterStudents = $conn->query($outerQuery . $filterQuery . $groupby);
Instead of group by, you can also use distinct, e.g.
select distinct student_pid
( select student_pid from ...
union all
select student_pid from ...
union all
select student_pid from ... ) as t
Collect the result in an array and then use array_unique
Although this is the least efficient, since it fetches all entries instead of the relevant parts.
I'm selecting data from 2 tables.
$sql = "SELECT tb1.id, tb2.name FROM tblA tbl1 LEFT JOIN tblB tbl2 ON tb1.id = tbl2.studentID ORDER BY tbl1.id DESC LIMIT 20";
$statement = $con_db->prepare($sql);
My question is now can I SELECT custom fields from tb1 and all fields in tb2? e.g.
$sql = "SELECT tb1.id, tb1.subject, tb2.(*) FROM ....";
You can write the code like below
$sql = "SELECT tb1.id, tb1.subject, tb2.* FROM tblA tbl1 LEFT JOIN tblB tbl2 ON tb1.id = tbl2.studentID ORDER BY tbl1.id DESC LIMIT 20";
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.
I have a problem in fetching data from mysql database tables.
I have two tables like table-1 and table-2 in below figure. How to get data from table-2 when pilotid is not equal to 1 in table-1.
I'm not sure, if I understand correctly, but this returns all rows of table-1, that do not have a matching entry in table-2. You can find the respective documentation of NOT EXISTS here.
SELECT *
FROM table-1 t1
WHERE NOT EXISTS( SELECT * FROM table-2 t2 WHERE t1.`Venueid` = t2.`Venueid` )
select a.venueid, a.name
from table2 a, table-1 b
where b.pilotid <> 1 and b.venueid = a.venueid;
SELECT Table_2.*
FROM Table_2
LEFT JOIN Table_1
ON Table_2.Venueid = Table_1.Venueid
WHERE Table_1.Venueid != 1
OR Table_1.Venueid NOT IN(1, 13, 15);
$sql = "select Venueid from Table1 where pilotid <> 1";
$data = mysql_query($sql);
while($row = mysql_fetch_assoc($data))
{
$ids[] = $row['Venueid'];
}
$sql2 = "select * from Table2 where venueid IN(".implode(',', $ids).")";
$data2 = mysql_query(sql2);
//$data2 contains the result-set resource;
My two tables look like this:
TABLE1 TABLE2
+--------------------+ +--------------------+
|field1|field2|field3| and |field2|field4|field5|
+--------------------+ +--------------------+
I am already running a SELECT query for TABLE1, and assorting all of the data into variables:
$query = "SELECT * FROM TABLE1 WHERE field2 = 2";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
if((!is_bool($result) || $result) && $num_rows) {
while($row = mysql_fetch_array($result))
{
$field1 = $row['field1'];
$field2 = $row['field2'];
$field3 = $row['field3'];
}
}
What I want to do is get the data from 'field4' on TABLE2 and add it to my variables. I would want to get field4 WHERE field2 = 2
SELECT
t1.*,
t2.field4
FROM
TABLE1 AS t1,
TABLE2 AS t2
WHERE
t1.field2 = 2
AND
t1.field2 = t2.field2
You want to join the two table which can be done explicitly (using the JOIN operator or one of its variants) our implicitly (by SELECTing from multiple tables directly).
From your description, I suppose that you want to do the join where field2 in the two tables have the same value (2).
If TABLE2 will not always provide a valid join candidate but you still want the data from TABLE1, you should use a LEFT JOIN (giving NULL for field4 where nothing matched):
SELECT
t1.*,
t2.field4
FROM
TABLE1 AS t1
LEFT JOIN
TABLE2 AS t2
ON
t1.field2 = t2.field2
WHERE
t1.field2 = 2
http://en.wikipedia.org/wiki/Join_%28SQL%29
You need to use a JOIN:
SELECT TABLE1.*,TABLE2.field4 FROM TABLE1 JOIN TABLE2 ON TABLE1.field2=TABLE2.field2 WHERE TABLE1.field2=2;
hi try the following as ur query.
It is not preferred all the time to use * in the select query
SELECT T1.field1,T1.field2,T1.field3, T2.field4 FROM TABLE1 AS T1 INNER JOIN TABLE2 AS T2 ON T1.field2=T2.field2 WHERE field2 = 2