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";
Related
Already, when I was using mysql_query, I used FOUND_ROWS() function like this:
$query = 'SELECT SQL_CALC_FOUND_ROWS * FROM Users';
mysql_query($query);
$query = 'SELECT FOUND_ROWS()';
mysql_query($query);
Now I use PDO and here is my query:
$sth = $this->dbh->prepare("SELECT SQL_CALC_FOUND_ROWS *,
u.id user_id,
u.avatar,
u.date_time,
CONCAT(u.user_fname, ' ', u.user_lname) name,
sum($this->table_alias.vote_value) vote_value,
sum($this->table_alias.score) score,
$category_in_the_select AS tc $tag_in_the_select AS tt
FROM users u
JOIN reputations $this->table_alias ON u.id = $this->table_alias.owner_id $query_join
WHERE $time_limitation $query_where
GROUP BY user_id, u.avatar, u.date_time, name, tc, tt
ORDER BY score DESC, vote_value DESC
LIMIT :j, $this->per_page");
$this->parameters[":j"] = $j;
$sth->execute($this->parameters);
$users = $sth->fetchAll(PDO::FETCH_ASSOC);
As you can see, I've used SQL_CALC_FOUND_ROWS in the SELECT statement and I want to know how can I use FOUND_ROWS()?
All you need to do is run a ->query('SELECT FOUND_ROWS()') after your original execute.
$sth = $this->dbh->prepare("SELECT SQL_CALC_FOUND_ROWS *,
u.id user_id,
u.avatar,
u.date_time,
CONCAT(u.user_fname, ' ', u.user_lname) name,
sum($this->table_alias.vote_value) vote_value,
sum($this->table_alias.score) score,
$category_in_the_select AS tc $tag_in_the_select AS tt
FROM users u
JOIN reputations $this->table_alias ON u.id = $this->table_alias.owner_id $query_join
WHERE $time_limitation $query_where
GROUP BY user_id, u.avatar, u.date_time, name, tc, tt
ORDER BY score DESC, vote_value DESC
LIMIT :j, $this->per_page");
$this->parameters[":j"] = $j;
$sth->execute($this->parameters);
$users = $sth->fetchAll(PDO::FETCH_ASSOC);
$count = $this->dbh->query('SELECT FOUND_ROWS()')->fetchColumn();
How can I connect to 4 tables in a single query using forign key IDs?
I know how to connect to two 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);
Try like this :
select t1.ID, t2.studentID, t3.aID, t4.bID
from table1 as t1
left join tbl2 as t2 on t2.studentID = t1.id
left join tbl3 as t3 on t3.aID = t1.id
left join tbl4 as t4 on t4.bID = t1.id
I'm new to mysql and I have a problem with selecting data from mysql database:
$post_id = 3;
$current_user_id = 1;
$query = "SELECT `comments`.*, `users`.`username`, `users`.`image` FROM (`comments`)
LEFT JOIN `users` ON `comments`.`user_id` = `users`.`id`
WHERE `comments`.`post_id` = '$post_id'
AND `comments`.`status` = 1
AND `users`.`status` = 1
ORDER BY `comments`.`date` desc";
This select, selects all approved comments from database, but also, in this select, I need all unapproved comments of $current_user_id,
Result must look like:
[all approved post comments] + [all unapproved post comments of $current_user_id]
not sure if this will really work, but just give a try and see. I can't test the query since we don't have the schema.
SELECT `comments`.*, `users`.`username`, `users`.`image` FROM (`comments`)
LEFT JOIN `users` ON `comments`.`user_id` = `users`.`id`
WHERE `comments`.`post_id` = '$post_id'
AND (
(`comments`.`status` = 1 AND `users`.`status` = 1)
OR
( `comments`.`status` = 0 AND `users`.`id`= '$current_user_id' )
)
ORDER BY `comments`.`date` desc
What I thought is select all the approved comments OR ( comments that are not approved, but from this user ). You might have to alter the query till you get what you really needed, I'm just giving you the idea, not the exact query. Hope this will help you.
SELECT `comments`.*, `users`.`username`, `users`.`image` FROM (`comments`)
LEFT JOIN `users` ON `comments`.`user_id` = `users`.`id`
WHERE `comments`.`post_id` = '$post_id'
AND `comments`.`status` = 1
AND `users`.`status` = 1
UNION ALL
SELECT `comments`.*, `users`.`username`, `users`.`image` FROM (`comments`)
LEFT JOIN `users` ON `comments`.`user_id` = `users`.`id`
WHERE `comments`.`post_id` = '$post_id'
AND `comments`.`status` = 0
AND `users`.`id` = '$current_user_id'
ORDER BY `comments`.`date` desc
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'];
}
How do i combine the counts from all the tables being used in a UNION query. This is what i have:
$query = "SELECT COUNT(*) as num
from table_one LEFT JOIN table_constant on table_one.c_id
= table_constant.c_id
where table_constant.user_id = '$uid'
UNION
SELECT COUNT(*) as num
from table_two LEFT JOIN table_constant on table_two.c_id
= table_constant.c_id
where table_two.added_by = '$uid'
UNION
SELECT COUNT(*) as num
from table_three LEFT JOIN table_constant ON table_three.c_id
= table_constant.c_id
where table_constant.user_id = '$uid'
UNION
SELECT COUNT(*) as num
from table_four LEFT JOIN table_constant ON table_four.c_id
= table_constant.c_id
where table_constant.user_id = '$uid'
ORDER BY date_time_added DESC";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];
Wrap the whole thing in yet another query and do the summation there.
SELECT sum(num)
FROM ( ... union queries here ...) as subquery;
Or loop over the returned rows in PHP and do the summation yourself.
Did you try to put count outside and apply it on sub query containing all tables union result.
SELECT COUNT(*) FROM (SELECT ...) as abc
Or try this out
Select mytable .userid, sum(mytable .subcount) as totalcount from
(
select userid, count(*) as subcount from table1 group by userid
union all
select userid, count(*) as subcount from table2 group by userid
)
as mytable
group by mytable .userid
or try using FULL OUTER JOIN instead of union it will give you the same result..
SELECT Count(UserID), UserId
FROM MyTable1
GROUP BY MyTable1.UserID
UNION
SELECT Count(UserID), UserId
FROM MyTable2
FULL OUTER JOIN MyTable2 ON (MyTable1.UserId=MyTable2.UserId)
GROUP BY MyTable2.UserID
Updated answer:
IF Your query is working fine follow my first option that i gave means
select count(*) from (your query) as pagecount..
Then your query would be like this.....
select count(*) from
(
SELECT COUNT(*) as num
from table_one LEFT JOIN table_constant on table_one.c_id
= table_constant.c_id
where table_constant.user_id = '$uid'
UNION
SELECT COUNT(*) as num
from table_two LEFT JOIN table_constant on table_two.c_id
= table_constant.c_id
where table_two.added_by = '$uid'
UNION
SELECT COUNT(*) as num
from table_three LEFT JOIN table_constant ON table_three.c_id
= table_constant.c_id
where table_constant.user_id = '$uid'
UNION
SELECT COUNT(*) as num
from table_four LEFT JOIN table_constant ON table_four.c_id
= table_constant.c_id
where table_constant.user_id = '$uid'
ORDER BY date_time_added DESC") as pagecount
There must be a better way to write that :/. A union is very powerful, but you are calling 4 selects in a single query, and if that is run every page, it will really hurt performance.
To answer you question, something like:
SELECT
SUM (mnTbl.num) as sumNum
FROM
(
SELECT
COUNT(*) as num
FROM
table_one
LEFT JOIN
table_constant
ON
table_one.c_id = table_constant.c_id
WHERE
table_constant.user_id = '$uid'
UNION
SELECT
COUNT(*) as num
FROM
table_two
LEFT JOIN
table_constant
ON
table_two.c_id = table_constant.c_id
WHERE
table_two.added_by = '$uid'
UNION
SELECT
COUNT(*) as num
FROM
table_three
LEFT JOIN
table_constant
ON
table_three.c_id = table_constant.c_id
WHERE
table_constant.user_id = '$uid'
UNION
SELECT
COUNT(*) as num
FROM
table_four
LEFT JOIN
table_constant
ON
table_four.c_id = table_constant.c_id
WHERE
table_constant.user_id = '$uid'
) as mnTbl
ORDER BY
date_time_added DESC