subtraction within unions or joins in table php/mysql - php

i have an income and expense table. i want to select them both in a single query and get the difference between the income.amount field and expense.amount field grouping by month. so that the query will return a result whereby the total of expenses amount from the total of income amount basing the calculation on month. i used used two methods but non worked. find the below:
SELECT *, count(*), sum(`transaction`.amount) as tiamount, sum(expenditure.amount) as teamount, monthname(`transaction`.date) as mni, monthname(expenditure.date) as mne
FROM `transaction`, expenditure
WHERE month(expenditure.`date`)=month(`transaction`.`date`) and month(`transaction`.`date`)=month(expenditure.`date`)
GROUP BY monthname(`transaction`.date) ORDER BY `transaction`.date Desc
the other is :
SELECT count(*), `transaction`.date, sum(`transaction`.amount) as tiamount, sum(`transaction`.amount - expenditure.amount) as diff, monthname(`transaction`.date) as mni
FROM `transaction` left join expenditure on monthname(`transaction`.date) = monthname(expenditure.date)
UNION ALL
SELECT count(*), expenditure.date, sum(expenditure.amount) as teamount, sum(`transaction`.amount - expenditure.amount) as diff, monthname(expenditure.date) as mne
FROM expenditure left join `transaction` on monthname(`transaction`.date) = monthname(expenditure.date)
any help would be appreciated. Thanks.

ok thanks all. i solved the issue. each time the page is loaded, it checks to see if the table balance2 already exist, if it does it is dropped and recreated on the fly
mysql_query($query_truncate = "drop table IF EXISTS balance2");
$create= "CREATE TABLE balance2 (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
`count` VARCHAR( 50 ) NOT NULL DEFAULT '',
`month` VARCHAR( 50 ) NOT NULL DEFAULT '',
`amount` VARCHAR( 50 ) NOT NULL DEFAULT '',
`amount2` VARCHAR( 20 ) NOT NULL ,
`type` VARCHAR( 20 ) NOT NULL ,
`date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
PRIMARY KEY ( `id` )
) ENGINE = INNODB DEFAULT CHARSET = latin1";
mysql_query($create);
Then in used the INSERT INTO...SELECT...UNION SELECT....
to select from the expenses and income table to insert into the newly created balance table.
query_merge = "INSERT INTO balance2 (count, date, amount, amount2, month, type)
(SELECT count(*), t.date, sum(t.amount),0 ,monthname(t.date), 'income'
FROM `transaction` as t group by month(t.date))
UNION
(SELECT count(*), e.date, 0,sum(e.amount) as teamount, monthname(e.date) as mne, 'expense'
FROM expenditure as e group by month(e.date))";
this worked perfectly as it gave me all the results i wanted.
Thought it might help someone else. see ya

Related

Counting from multiple tables and return all count values in one query slow

I want to counting from multiple tables and return all count values in one query. I've created SQL and It feels like this query is slow. Is this the best way to do as i said? If not, please suggest me for better solutions. Because sometimes it took more than 15 seconds to finish querying. Thank you.
And here is my database approximate info.
table post 400 rows.
table comment 3000 rows.
table like 1000 rows.
table view 6000 rows.
SQL
SELECT p.*,
COUNT(c.id) as commentCount,
COUNT(l.id) as likeCount,
COUNT(c.id) as viewCount,
FROM post p
LEFT JOIN comment c
ON (p.id = c.postid)
LEFT JOIN like l
ON (p.id = l.postid)
LEFT JOIN view v
ON (p.id = v.postid)
GROUP BY u.id
ORDER BY postCount DESC
TABLE post
CREATE TABLE `post` (
`id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`details` VARCHAR( 500 ) NOT NULL ,
`datetime` DATETIME NOT NULL
) ENGINE = MYISAM ;
TABLE comment
CREATE TABLE `comment ` (
`id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`postid` INT( 10 ) NOT NULL ,
`details` VARCHAR( 500 ) NOT NULL ,
`datetime` DATETIME NOT NULL
) ENGINE = MYISAM ;
TABLE like
CREATE TABLE `like` (
`id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`postid` INT( 10 ) NOT NULL ,
`datetime` DATETIME NOT NULL
) ENGINE = MYISAM ;
TABLE view
CREATE TABLE `view` (
`id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`postid` INT( 10 ) NOT NULL ,
`ip` VARCHAR( 30) NOT NULL ,
`datetime` DATETIME NOT NULL
) ENGINE = MYISAM ;
The query is slow because you are aggregating along multiple dimensions. This greatly multiplies the size of the data. One solution is to pre-aggregate the data:
SELECT p.*, c.cnt as commentCount, l.cnt as likeCount, v.cnt as viewCount,
FROM post p LEFT JOIN
(select c.postid, count(*) as cnt
from comment c
group by c.postid
) c
ON p.id = c.postid LEFT JOIN
(select l.postid, count(*) as cnt
from like l
group by l.postid
) l
ON p.id = l.postid LEFT JOIN
(select v.postid, count(*) as cnt
from view v
group by v.postid
) v
ON p.id = v.postid
GROUP BY p.id
ORDER BY postCount DESC;
In addition, your query would return about the same values for the three counts -- and they would probably not be accurate.

Query to find 16 most clicked posts in last 30 days without duplicated categories

I have a seemingly simple task but I cannot seem to find an elegant solution using 1 query...
Problem:
I have a table of recorded 'clicks' on 'posts', where each post is part of a 'category'.
I want to find the 16 highest clicked posts in the last 30 days -- but I want to avoid duplicate categories.
It seems very simple actually, but I seem to be stuck.
I know how to get the most clicked in last 30, but I can't figure out how to avoid duplicate cats.
SELECT cat_id,
post_id,
COUNT(post_id) AS click_counter
FROM cs_coupon_clicks
WHERE time_of_click > DATE_SUB(NOW(), INTERVAL 30 DAY)
GROUP BY post_id
ORDER BY click_counter DESC
I tried to get creative/hacky with it... it's close but not correct:
SELECT cat_id,
Max(sort) AS sortid
FROM (SELECT cat_id,
post_id,
COUNT(post_id) AS click_counter,
CONCAT(COUNT(post_id), '-', post_id) AS sort
FROM cs_coupon_clicks
WHERE time_of_click > DATE_SUB(NOW(), INTERVAL 30 DAY)
GROUP BY cat_id, post_id) t1
GROUP BY cat_id
ORDER BY cat_id ASC
Any help would be greatly appreciated as I am not really a MySQL expert. I may end up just doing some PHP logic in the end, but I am very curious as to the correct way to approach a problem like this.
Thanks guys.
EDIT (structure):
CREATE TABLE `cs_coupon_clicks` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`src` varchar(255) NOT NULL DEFAULT '',
`cat_id` int(20) NOT NULL,
`post_id` int(20) NOT NULL,
`tag_id` int(20) NOT NULL,
`user_id` int(20) DEFAULT NULL,
`ip_address` char(30) DEFAULT NULL,
`referer` varchar(255) NOT NULL,
`browser` varchar(10) DEFAULT NULL,
`server_var` text NOT NULL,
`time_of_click` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `cat_id` (`cat_id`),
KEY `post_id` (`post_id`),
KEY `user_id` (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
TEMP WORKING SOLUTION (HACKY):
SELECT
cat_id,
MAX(sort) AS sortid
FROM (
SELECT
cat_id,
post_id,
COUNT(post_id) AS click_counter,
RIGHT(Concat('00000000', COUNT(post_id), '-', post_id), 16) AS SORT
FROM cs_coupon_clicks
WHERE time_of_click > DATE_SUB(NOW(), INTERVAL 30 DAY)
GROUP BY cat_id, post_id
) AS t1
GROUP BY cat_id
ORDER BY sortid DESC
There is no easy single query solution to this problem, it's a group-wise maximum kind of problem based on a temporary table (the one with counts) that would require self-joins.
Assuming your database grows big enough (otherwise just go for your php logic) I would go for a statistics table, holding info about categories, posts and click counts:
CREATE TABLE `click_cnts` (
`cat_id` int(20) NOT NULL,
`post_id` int(20) NOT NULL,
`clicks` int(20) NOT NULL,
PRIMARY KEY (`cat_id`,`post_id`),
KEY `cat_id` (`cat_id`,`clicks`)
)
and fill it using the same query as the first one in the question:
INSERT INTO click_cnts(cat_id, post_id, clicks)
SELECT cat_id, post_id, COUNT(post_id) AS click_counter
FROM cs_coupon_clicks
WHERE time_of_click > NOW() - INTERVAL 30 DAY
GROUP BY cat_id,post_id
You could update this table using triggers or running update query periodically (do users really need info up to the very last second? probably not...) and save a lot of processing as finding most clicks for each category on indexed table requires a lot less time using a classic group-wise max approach:
SELECT cg.cat_id, cu.post_id, cg.most_clicks
FROM
( SELECT cat_id, max(clicks) as most_clicks FROM click_cnts
GROUP BY cat_id ) cg
JOIN click_cnts cu
ON cg.cat_id = cu.cat_id
AND cu.post_id = ( SELECT cc.post_id FROM click_cnts cc
WHERE cc.cat_id = cg.cat_id
AND cc.clicks = cg.most_clicks
LIMIT 1 )
ORDER BY cg.most_clicks DESC
LIMIT 16
Shot in the dark here. Did you try Select DISTINCT cat_id

Complex Queries - Get people with the most referrals

I am trying to make some sort of SQL Query where I only get the 10 people with the most referrals, but with minimum 1 referral.
My Table looks like this:
CREATE TABLE IF NOT EXISTS `beta_list` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`email` varchar(250) NOT NULL,
`referrer` int(10) NOT NULL,
`referral_code` int(10) NOT NULL,
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
I have tried something like this:
SELECT
email,
referral_code as refcode,
(SELECT COUNT(*) FROM beta_list WHERE referrer=refcode) as referrals
FROM
beta_list
WHERE
referrals > 0
ORDER BY
referrals DESC
LIMIT
10
But it just says "Unknown column 'referrals' in 'where clause'".
I am no sql guru, I am only just beginning to learn more complex sql queries, so any help on how to achieve something like this would be deeply appreciated!
Cheers!
Try this - Add an outer query to extract the results from inner query -
select ref.email, ref.refcode, ref.referrals from
(
SELECT
email,
referral_code as refcode,
(SELECT COUNT(*) FROM beta_list WHERE referrer=refcode) as referrals
FROM
beta_list
) as ref
WHERE
ref.referrals > 0
ORDER BY
ref.referrals DESC
LIMIT
10
Give this a go:
SELECT email,referral_code as refcode,count(*) as referrals
FROM beta_list
WHERE referrer = referral_code
GROUP BY email,referral_code
ORDER BY referrals DESC
LIMIT 10;

MySQL Left Join, Group By, Order By, Limit = Terrible Performance

I am currently developing a an application to allow users to search through a database of documents using various paramaters and returning a set of paged results. I am building it in PHP/MySQL, which is not my usual development platform, but its been grand so far.
The problem I am having is that in order to return a full set of results I have to use LEFT JOIN on every table, which completely destroys my performance. The person who developed the database has said that the query I am using will return the correct results, so thats what I have to use. The query is below, I am by no means an SQL Guru and could use some help on this.
I have been thinking that it might be better to split the query into sub-queries? Below is my current query:
SELECT d.title, d.deposition_id, d.folio_start, d.folio_end, pl.place_id, p.surname, p.forename, p.person_type_id, pt.person_type_desc, p.age, d.manuscript_number, dt.day, dt.month, dt.year, plc.county_id, c.county_desc
FROM deposition d
LEFT JOIN person AS p ON p.deposition_id = d.deposition_id
LEFT JOIN person_type AS pt ON p.person_type_id = pt.person_type_id
LEFT JOIN place_link AS pl ON pl.deposition_id = d.deposition_id
LEFT JOIN date AS dt ON dt.deposition_id = d.deposition_id
LEFT JOIN place AS plc ON pl.place_id = plc.place_id
LEFT JOIN county AS c ON plc.county_id = c.county_id
WHERE 1 AND d.manuscript_number = '840'
GROUP BY d.deposition_id ORDER BY d.folio_start ASC
LIMIT 0, 20
Any help or guidance would be greatly appreciated!
Deposition Table:
CREATE TABLE IF NOT EXISTS `deposition` (
`deposition_id` varchar(11) NOT NULL default '',
`manuscript_number` int(10) NOT NULL default '0',
`folio_start` varchar(4) NOT NULL default '0',
`folio_end` varchar(4) default '0',
`page` int(4) default NULL,
`deposition_type_id` int(10) NOT NULL default '0',
`comments` varchar(255) default '',
`title` varchar(255) default NULL,
PRIMARY KEY (`deposition_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Date Table
CREATE TABLE IF NOT EXISTS `date` (
`deposition_id` varchar(11) NOT NULL default '',
`day` int(2) default NULL,
`month` int(2) default NULL,
`year` int(4) default NULL,
PRIMARY KEY (`deposition_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Person_Type
CREATE TABLE IF NOT EXISTS `person_type` (
`person_type_id` int(10) NOT NULL auto_increment,
`person_type_desc` varchar(255) NOT NULL default '',
PRIMARY KEY (`person_type_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=59 ;
Seems that you want to select one person, place etc. per deposition.
The query you wrote will return you this, but it's not guaranteed which one will it return, and the query is inefficient.
Try this:
SELECT d.title, d.deposition_id, d.folio_start, d.folio_end, pl.place_id, p.surname, p.forename, p.person_type_id, pt.person_type_desc, p.age, d.manuscript_number, dt.day, dt.month, dt.year, plc.county_id, c.county_desc
FROM deposition d
LEFT JOIN
person p
ON p.id =
(
SELECT id
FROM person pi
WHERE pi.deposition_id = d.deposition_id
ORDER BY
pi.deposition_id, pi.id
LIMIT 1
)
LEFT JOIN
place_link AS pl
ON pl.id =
(
SELECT id
FROM place_link AS pli
WHERE pli.deposition_id = d.deposition_id
ORDER BY
pli.deposition_id, pi.id
LIMIT 1
)
LEFT JOIN
date AS dt
ON dt.id =
(
SELECT id
FROM date AS dti
WHERE dti.deposition_id = d.deposition_id
ORDER BY
dti.deposition_id, pi.id
LIMIT 1
)
LEFT JOIN
place AS plc
ON plc.place_id = pl.place_id
LEFT JOIN
county AS c
ON c.county_id = plc.county_id
WHERE d.manuscript_number = '840'
ORDER BY
d.manuscript_number, d.folio_start
LIMIT 20
Create an index on deposition (manuscript_number, folio_start) for this to work fast
Also create a composite index on (deposition_id, id) on person, place_link and date.
The poor performance is almost certainly from lack of indexes. Your deposition table doesn't have any indexes, and that probably means the other tables you're referencing don't have any either. You can start by adding an index to your deposition table. From the MySQL shell, or phpMyAdmin, issue the following query.
ALTER TABLE deposition ADD INDEX(deposition_id, manuscript_number);
You know you're on the right track if the query executes faster after adding the index. From there you might want to put indexes on the other tables on the referenced columns. For instance for this part of your query "LEFT JOIN person AS p ON p.deposition_id = d.deposition_id", you could try adding an index to the person table using.
ALTER TABLE person ADD INDEX(deposition_id);
You only need a LEFT JOIN if the joined table might not have a matching value. Is it possible in your database schema for a person to not have a matching person_type? Or deposition to not have a matching row in date? A place not have a matching county?
For any of those relationships that must exist for the result to make sense you can change the LEFT JOIN to an INNER JOIN.
These columns should have indexes (unique if possible):
person.deposition_id
date.deposition_id
place_link.deposition_id
place_link.place_id
The date table looks like a bad design; I can't think of a reason to have a table of dates instead of just putting a column of type date (or datetime) in the deposition table. And date is a terrible name for a table because it's a SQL reserved word.

MYSQL: How to find out the qty of unequipped items?

Here is my DB Structure:
CREATE TABLE IF NOT EXISTS `UserItems` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`user_id` int(10) unsigned NOT NULL,
`item_id` int(10) unsigned NOT NULL,
`qty` int(11) NOT NULL default '0'
) ;
CREATE TABLE IF NOT EXISTS `UserEquippedItems` (
`user_id` int(10) unsigned NOT NULL,
`user_item_id` bigint(20) unsigned NOT NULL
);
CREATE TABLE IF NOT EXISTS `UserFriendEquippedItems` (
`user_friend_id` int(10) unsigned NOT NULL,
`user_item_id` bigint(20) unsigned NOT NULL
);
UserItems keeps all the item inventory with quantity.
let say if I have 5 item (item id 123456). then the entry will be
(null, $userid, 123456, 5).
the qty is the quantity of all the entity with the same item_id.
However, some Users may equip the item. Some do not. If they equip it, it will be an entry in UserEquippedItems table.
Also, users' friends can equip the user's item too.
Sample Data:
UserItems:
id, user_id, item_id, qty
( 1, 4567, 123123123, 5)
( 2, 4567, 100010001, 2)
( 3, 4567, 100010099, 1)
UserEquippedItems: (user_item_id is UserItems.id)
user_id, user_item_id
( 4567, 1)
( 4567, 2)
UserFriendEquippedItems
(user_item_id is UserItems.id)
user_friend_id, user_item_id
( 4100, 1)
( 4100, 3)
So, how can I find out the quantity of items that are equipped?
and how can I find out the quantity of items that are NOT equipped ?
Side Story: Before, we have each individual UserItems as a single entry in the DB. e.g. for item_id = 123123123, if I have 5 of them, i have 5 entries in the DB. But then, our DB grows like crazy to 4 million UserItems records. that's why instead of having 5 entries, we only have one entry in the UserItems table, with the qty field for keep track how many in total. I don't know if it is the right approach, but I hope it can cut down 75% of the DB size.
Also, it was the query to get the unequipped items:
SELECT Items.id, count(UserItems.id) as numCount
FROM UserItems INNER JOIN
Items ON UserItems.active=1 AND
UserItems.item_id=Items.id AND
Items.active=1 AND
UserItems.user_id=$userId
WHERE NOT EXISTS (
SELECT UserEquippedItems.user_item_id
FROM UserEquippedItems
WHERE UserEquippedItems.user_item_id= UserItems.id
)
AND NOT EXISTS (
SELECT UserFriendsEquippedItems.user_item_id
FROM UserFriendsEquippedItems
WHERE UserFriendsEquippedItems.user_item_id= UserItems.id)
GROUP BY Items.id
Of course, this query doesn't work with the new schema. :)
This should work:
SELECT id, user_id, item_id, qty,
(SELECT COUNT(*) FROM UserEquippedItems uei
WHERE uei.user_item_id=ui.id) as qty_equipped,
(SELECT COUNT(*) FROM UserFriendEquippedItems ufei
WHERE ufei.user_item_id=ui.id) as qty_friend_equipped
FROM UserItems ui
Then to get the unequipped, on your client you can subtract the qty_equipped and qty_friend_equipped from qty.
A single query that just returns unequipped:
SELECT id, user_id, item_id, qty,
qty-
(SELECT COUNT(*) FROM UserEquippedItems uei
WHERE uei.user_item_id=ui.id)-
(SELECT COUNT(*) FROM UserFriendEquippedItems ufei
WHERE ufei.user_item_id=ui.id)
as qty_unequipped
FROM UserItems ui
You could combine those 2 queries above into one big query, but I image that would hurt performance, since it will run the sub-queries twice.
You can add a WHERE clause on the end of both of these to return results for a specific user / item.
To count the total number of items equipped:
SELECT count(*) FROM UserEquippedItems e JOIN UserItems i ON (i.id = e.user_item_id)
To find the total number of items of id $item_id equipped
SELECT count(*) FROM UserEquippedItems e WHERE e.user_item_id = $item_id
And to find the total number of items user $user_id has equipped (my first response, though probably not what you're after)
SELECT count(*) FROM UserEquippedItems WHERE user_id = $user_id

Categories