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
Related
I have a variable which is containing a dynamic query. Something like this:
$query = "SELECT id, subject FROM post1
UNION ALL
SELECT id, subject FROM post2
UNION ALL
SELECT id, subject FROM post3";
Also I have this query:
SELECT code FROM mytable WHERE id = :id
Now I want to join query above with that dynamic query. Here is my try:
SELECT t1.code t2.subject FROM mytable t1
LEFT JOIN ($query) t2 ON t1.col = t2.id
WHERE t1.id = :id
/*
SELECT t1.code t2.subject FROM mytable t1
LEFT JOIN (SELECT id, subject FROM post1
UNION ALL
SELECT id, subject FROM post2
UNION ALL
SELECT id, subject FROM post3) t2 ON t1.col = t2.id
WHERE t1.id = :id
*/
It works. But it takes a lot of time for huge data. How can I make it faster?
SELECT t1.code t2.subject FROM mytable t1
LEFT JOIN (SELECT id, subject FROM post1
JOIN mytable tt1 ON tt1.col = post1.id AND tt1.id=:id
UNION ALL
SELECT id, subject FROM post2
JOIN mytable tt2 ON tt2.col = post2.id AND tt2.id=:id
UNION ALL
SELECT id, subject FROM post3
JOIN mytable tt3 ON tt3.col = post3.id AND tt3.id=:id) t2 ON t1.col = t2.id
WHERE t1.id = :id
Just add the :id check to the internal querie4st to restrict amount of data selected.
You can use below query.
SELECT t1.code, t2.subject FROM (SELECT code, col FROM mytable WHERE id = :id ) t1
LEFT JOIN ($query) t2 ON t1.col = t2.id
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 below query, which I believe I have optimized as much as I possible could:
SELECT
c.forum_id as category_id,
c.forum_name as category_name,
t.forum_id as id,
t.forum_name as name,
t.forum_desc as description,
(SELECT COUNT(*) FROM forum_topics WHERE forum_id=t.forum_id AND topic_deleted=0) as topics_count,
(SELECT COUNT(*) FROM forum_posts WHERE forum_id=t.forum_id AND post_deleted=0) as posts_count,
(SELECT COUNT(*) FROM forum_posts WHERE topic_id=lp.topic_id AND post_deleted=0) as last_post_count,
lp.topic_id as last_post_topic_id,
lp.topic_title as last_post_topic_title,
lp.post_time as last_post_time,
lp.username as last_post_username,
lp.avatar
FROM forum_cats as t
JOIN forum_cats as c on c.forum_id = t.forum_type_id
left join (
SELECT
ft.topic_id,
ft.title as topic_title,
tmp.post_time,
u.username,
u.avatar,
fp.forum_id
FROM
forum_posts fp
join forum_topics ft on ft.topic_id = fp.topic_id
join users u on u.id = fp.userid
join (
select forum_id, max(`post_time`) `post_time`
from forum_posts fp
where fp.post_deleted = 0
group by forum_id
) as tmp on (fp.forum_id = tmp.forum_id and fp.post_time = tmp.post_time)
where post_deleted = 0 and ft.topic_deleted = 0
) as lp on lp.forum_id = t.forum_id
where t.forum_active = 1 and c.forum_active = 1
order by category_id, t.forum_id
This is the stats for each forum:
forum_cats has 20 rows
forum_topics has 900 rows
forum_posts has 9000 rows
users has 18000 rows
I have added index on all the columns that is being selected in above query. Yet it still takes more than 2 seconds to execute this.
What am I missing here?
Won't topics_count, posts_count & last_post_count be the same for every row? Wouldn't it be more efficient to pull these values separately, rather than make the same 3 calculations for every row you pull?
how can i combine these two queries in mysql?
select count(*) as entry_count from tbl_entries where user_id = x
and
select username, avatar from tbl_users where user_id = x
I want one result that combines the result of this 2 queries. Please help me guys!
Thanks!
select username,
avatar,
(select count(*) from tbl_entries where user_id = x) as entry_count
from tbl_users
where user_id = x
select username,
avatar,
(select count(*) from tbl_entries where user_id = x) AS cnt
from tbl_users
where user_id = x
try this one:
SELECT a.username,
a.avatar,
COUNT(*) as entry_count,
FROM tbl_Users a LEFT JOIN tbl_entries b ON
a.user_ID = b.user_ID
WHERE a.user_ID = x
GROUP BY a.username
Try this:
SELECT U.username, U.avatar,
count(*) AS entry_count
FROM tbl_users AS U
LEFT JOIN tbl_entries AS E ON U.user_id = E.user_id
WHERE user_id = x
GROUP BY U.user_id;
SELECT username,
avatar,
(select count(*) from tbl_entries where user_id = U.user_id) AS cnt
FROM tbl_users AS U
WHERE user_id = x
this is my query
SELECT a.id,
a.venue_id,
a.user_id,
m1.profilenam AS user_profilename,
m1.photo_thumb AS user_photo_thumb,
m2.profilenam AS venue_profilename,
m2.photo_thumb AS venue_photo_thumb
FROM announce_arrival AS a
INNER JOIN members AS m1
ON a.user_id = m1.mem_id
INNER JOIN members AS m2
ON a.venue_id = m2.mem_id
GROUP BY a.venue_id, a.user_id
LIMIT 0,10
ORDER BY date DESC,
time DESC
How can i use count(*) on this query,i use like this
SELECT DISTINCT COUNT(*)
FROM announce_arrival AS a
INNER JOIN members as m1 ON (a.user_id = m1.mem_id)
INNER JOIN members as m2 ON (a.venue_id= m2.mem_id)
GROUP BY a.venue_id, a.user_id LIMIT 0,10 ORDER BY date DESC,time DESC;
but its showing
COUNT(*)
7
3
1
i want total count .
You can wrap your query into select count(*), like this:
SELECT COUNT(*) FROM
(SELECT a.id,a.venue_id, a.user_id, m1.profilenam as
user_profilename,m1.photo_thumb AS user_photo_thumb,m2.profilenam AS
venue_profilename, m2.photo_thumb AS venue_photo_thumb FROM announce_arrival
AS a INNER JOIN members as m1 ON (a.user_id = m1.mem_id) INNER JOIN members
as m2 ON (a.venue_id= m2.mem_id) GROUP BY a.venue_id, a.user_id)
You can wrap your query in another SELECT:
SELECT COUNT(*) AS total FROM (
SELECT DISTINCT COUNT(*)
FROM announce_arrival AS a
INNER JOIN members as m1 ON (a.user_id = m1.mem_id)
INNER JOIN members as m2 ON (a.venue_id= m2.mem_id)
GROUP BY a.venue_id, a.user_id LIMIT 0,10 ORDER BY date DESC,time DESC) AS t
or if you want sum of all DISTINCT COUNT(*) try:
SELECT SUM(cnt) AS total FROM (
SELECT DISTINCT COUNT(*) AS cnt
FROM announce_arrival AS a
INNER JOIN members as m1 ON (a.user_id = m1.mem_id)
INNER JOIN members as m2 ON (a.venue_id= m2.mem_id)
GROUP BY a.venue_id, a.user_id LIMIT 0,10 ORDER BY date DESC,time DESC) AS t
From docs about found_rows():
To obtain this row count, include a
SQL_CALC_FOUND_ROWS option in the
SELECT statement, and then invoke
FOUND_ROWS() afterward:
mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name WHERE id > 100 LIMIT 10;
mysql> SELECT FOUND_ROWS();
The second SELECT returns a number
indicating how many rows the first
SELECT would have returned had it been
written without the LIMIT clause.
I.e., add SQL_CALC_FOUND_ROWS after SELECT in your first query and replace second query with SELECT FOUND_ROWS().
Get rid of GROUP BY, LIMIT and ORDER. They are useless and don't make sense (especially LIMIT) if you need a total count. DISTINCT doesn't make sense either.
SELECT COUNT(*)
FROM announce_arrival AS a
INNER JOIN members as m1 ON (a.user_id = m1.mem_id)
INNER JOIN members as m2 ON (a.venue_id= m2.mem_id)
Try this
SELECT COUNT(a.user_id)
FROM announce_arrival AS a
INNER JOIN members as m1 ON (a.user_id = m1.mem_id)
INNER JOIN members as m2 ON (a.venue_id= m2.mem_id)
GROUP BY a.user_id LIMIT 0,10;
If you are using count no need to give order by