MySQL - Correct approach event counting - mysql

I want to list users which have a particular event count but I'm confused on which approach to take.
This is the database table:
CREATE TABLE `event` (
`event_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`visitor_id` int(11) DEFAULT NULL,
`key` varchar(200) DEFAULT NULL,
`value` text,
`label` varchar(200) DEFAULT '',
`datetime` datetime DEFAULT NULL,
PRIMARY KEY (`event_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
INSERT INTO `event` (`event_id`, `visitor_id`, `key`, `value`, `label`, `datetime`)
VALUES
(1, 1, 'LOGIN', NULL, '', NULL),
(2, 2, 'LOGIN', NULL, '', NULL),
(3, 1, 'VIEW_PAGE', 'HOTEL', '', NULL),
(4, 2, 'VIEW_PAGE', 'HOTEL', '', NULL),
(5, 1, 'PURCHASE_HOTEL', NULL, '', NULL);
CREATE TABLE `visitor` (
`visitor_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`datetime` datetime DEFAULT NULL,
PRIMARY KEY (`visitor_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
INSERT INTO `visitor` (`visitor_id`, `datetime`)
VALUES
(1, NULL),
(2, NULL);
and this is my approach:
SELECT DISTINCT
t1.`visitor_id`
FROM
`visitor` t1
JOIN `event` t2 on t1.visitor_id = t2.visitor_id AND t2.`key` = 'LOGIN'
JOIN `event` t3 on t1.visitor_id = t3.visitor_id AND t3.`key` = 'VIEW_PAGE' AND t3.`value` = 'HOTEL'
WHERE ( SELECT COUNT(*) FROM `event` WHERE `event`.`key` = 'PURCHASE_HOTEL' ) > 0
this should only list visitor 1 but it does actually list visitor 2 too which does not have the PURCHASE_HOTEL event.
As you can imagine, there will be more "rules" like all the JOIN events for each particular case. Can we correct and improve this somehow?
BONUS:
What is the name of this approach?

I think this is a "set-within-sets" query. I like using aggregation with a having clause for this type of query. The following checks the three conditions you are looking for:
select visitor_id
from event e
group by visitor_id
having sum(e.key = 'LOGIN') > 0 and
sum(e.key = 'VIEW_PAGE' and e.value = 'HOTEL') > 0 and
sum(e.key = 'PURCHASE_HOTEL') > 0;
The first condition in the having clause counts the number of LOGIN records and is true when at least one is found. (If you want exactly one, change > 0 to = 0.)
The second condition checks the viewing of the hotel page.
The third counts the number of hotel purchases.

Related

MySQL Query - How do I get a SUM with GROUP BY and WHERE condition and use LEFT OUTER JOIN?

I'm not sure how to get the result that I expect and I've tried everything. I also need to be able to get the expected results using one query.
Here are the two tables with some sample data:
--
-- Table structure for table `accounts`
--
CREATE TABLE IF NOT EXISTS `accounts` (
`id` int(11) NOT NULL,
`name` varchar(200) NOT NULL,
`type_id` int(11) NOT NULL,
`description` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
--
-- Data for table `accounts`
--
INSERT INTO `accounts` (`id`, `name`, `type_id`, `description`) VALUES
(100, 'DUES', 0, NULL),
(101, 'NET WEEKLY PAYROLL', 0, NULL),
(111, 'FEDERAL TAX DEPOSITS', 0, 'tax stuff'),
(113, 'UNITED ASSOCIATION PAYMENTS', 0, NULL),
(114, 'OFFICERS MEETING ALLOWANCES', 0, NULL);
--
-- Table structure for table `checks`
--
CREATE TABLE IF NOT EXISTS `checks` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`batch_id` int(11) DEFAULT NULL,
`entry_date` date NOT NULL,
`account_id` int(11) NOT NULL,
`amount` decimal(10,2) NOT NULL,
`description` varchar(200) DEFAULT NULL,
`posted` tinyint(4) NOT NULL DEFAULT '0',
`vendor_id` int(11) DEFAULT NULL,
`check_num` int(11) NOT NULL,
`voided` tinyint(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
--
-- Dumping data for table `checks`
--
INSERT INTO `checks` (`id`, `batch_id`, `entry_date`, `account_id`, `amount`, `description`, `posted`, `vendor_id`, `check_num`, `voided`) VALUES
(1, NULL, '2013-01-21', 111, '77.44', 'Last Year', 0, 1, 100, 0),
(2, NULL, '2014-01-21', 111, '521.11', 'Test Stuff', 0, 1, 101, 0),
(3, NULL, '2014-01-20', 101, '121.11', 'More Tests', 0, 1, 222, 0),
(4, NULL, '2014-01-02', 101, '150.00', 'test', 0, 4, 213, 0);
I want to create a list of all accounts with a month-to-date sum as an added field. Here is the query to get the month-to-date sum without joining the accounts table:
SELECT *, SUM(amount) as mtd FROM `checks` WHERE `entry_date` > '2014-01-01' GROUP BY `account_id`
... and here is what i used to get all the accounts joined to checks table:
SELECT * FROM `accounts` LEFT OUTER JOIN `checks` ON `checks.account_id` = `accounts.id`
I just can't seem to combine these two correctly to get the expected results. Please help!
I think this solves your problem:
SELECT a.*, SUM(c.amount) as mtd
FROM accounts a left outer join
checks c
ON (a.id = c.account_id) and c.entry_date >= '2014-01-01'
GROUP BY a.account_id;
This will return all accounts, even those with no activity in January. I changed the date condition to >=, because that "feels" better as a month-to-date cutoff.

How to get multiple rows with order by and limit

I have a mysql table, the structure of that table is as follows:
CREATE TABLE IF NOT EXISTS `salary_log` (`salary_id` int(11) NOT NULL AUTO_INCREMENT, `salary` int(11) NOT NULL, `name` varchar(200) NOT NULL, `create_date` date NOT NULL, PRIMARY KEY (`salary_id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
INSERT INTO `salary_log` (`salary_id`, `salary`, `name`, `create_date`) VALUES (1, 5000, 'a', '2013-05-02'), (2, 8000, 'a', '2013-05-08'), (3, 4500, 'b', '2013-05-10'), (4,6000, 'c', '2013-05-08'), (5, 8000, 'b', '2013-05-17');
From the above table i need to fetch the latest records of each employee. I tried below query :
SELECT * FROM `salary_log` GROUP BY `name` ORDER BY `create_date` DESC;
But it returns be wrong data. Can someone please help me in this.
Try this:
SELECT s1.*
FROM `salary_log` AS s1
INNER JOIN
(
SELECT name, MAX(create_date) AS maxDate
FROM salary_log
GROUP BY `name`
) AS s2 ON s1.name = s2.name
AND s1.create_date = s2.maxDate;

right join query returning null value records

CREATE TABLE IF NOT EXISTS `tweets_comment_tbl` (
`tweet_comment_id` int(12) NOT NULL AUTO_INCREMENT,
`tweet_id` int(12) NOT NULL,
`tweets_comment` text NOT NULL,
`created` int(12) NOT NULL,
`changed` int(12) NOT NULL,
`uid` int(12) NOT NULL,
`userip` varchar(20) NOT NULL,
`referer` text NOT NULL,
`status` int(2) NOT NULL DEFAULT '1',
PRIMARY KEY (`tweet_comment_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
--
-- Dumping data for table `tweets_comment_tbl`
--
INSERT INTO `tweets_comment_tbl` (`tweet_comment_id`, `tweet_id`, `tweets_comment`, `created`, `changed`, `uid`, `userip`, `referer`, `status`) VALUES
(1, 1, 'COMMENT USER1', 1319395671, 1319395671, 3, '127.0.0.1', 'http://localhost/drupal_tutorial/', 1),
(2, 2, 'comment admin user', 1319395724, 1319395724, 1, '127.0.0.1', 'http://localhost/drupal_tutorial/node', 1),
(3, 2, 'USER COMMENTING HIS COMMENT', 1319395838, 1319395838, 3, '127.0.0.1', 'http://localhost/drupal_tutorial/', 1),
(4, 2, 'ADMIN COMMENTING FOR HIS COMMENT', 1319395865, 1319395865, 1, '127.0.0.1', 'http://localhost/drupal_tutorial/node', 1),
(5, 2, 'dddCOMMENT USER1: ADMIN DOING COMMENT FOR STATUS UPDATE1', 1319395905, 1319395905, 1, '127.0.0.1', 'http://localhost/drupal_tutorial/node', 1);
my second table
CREATE TABLE IF NOT EXISTS `tweets_tbl` (
`tweet_id` int(11) NOT NULL AUTO_INCREMENT,
`tweets` text NOT NULL,
`created` int(12) NOT NULL,
`changed` int(12) NOT NULL,
`uid` int(12) NOT NULL,
`userip` varchar(20) NOT NULL,
`referer` text NOT NULL,
`status` int(2) NOT NULL DEFAULT '1',
PRIMARY KEY (`tweet_id`),
KEY `tweet_id` (`tweet_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `tweets_tbl`
--
INSERT INTO `tweets_tbl` (`tweet_id`, `tweets`, `created`, `changed`, `uid`, `userip`, `referer`, `status`) VALUES
(1, 'STATUS UPDATE 1', 1319395633, 1319395633, 1, '127.0.0.1', 'http://localhost/drupal_tutorial/node', 1),
(2, 'Status update user1', 1319395696, 1319395696, 3, '127.0.0.1', 'http://localhost/drupal_tutorial/node', 1);
Trying join query
SELECT ut.picture as picture, tct.tweet_id as tweet_id, tct.tweets_comment as tweets_comment, tct.changed
FROM tweets_comment_tbl tct
RIGHT JOIN users as ut ON tct.uid=ut.uid AND tct.tweet_id=2 AND tct.status = 1
order by tct.created desc
return records for above query
picture tweet_id tweets_comment changed
1 COMMENT USER1 1319395671
NULL NULL NULL
picture-1.png NULL NULL NULL
actually what i expected is
picture tweet_id tweets_comment changed
1 COMMENT USER1 1319395671
Why query has been returning NULL records, i am not sure where i made mistake in join query.
The query returns all rows from users and joins tweets_comment_tbl based on condition in ON. If no records in tweets_comment_tbl matches userid, the record from users still included into recordset. You probably need INNER JOIN if you want to see just users with comments.
Side note :
I think it's almost always possible to avoid RIGHT JOIN; queries with LEFT JOIN are much easier to read and understand.

Mysql - Need to get latest of table A when referenced from table B

I'm building a bug tracker type tool for kicks.
I'm having probs with a small prob relating to version control of my data.
I have a table 'action' where I store all the data about the action (desription, who entered it, status etc). I also have a action_status table where each time the status is changed (from not asigned, in progress, complete etc) it is logged here..
What I can't seem to do is list the actions with their latest status value.
You'll note that the status table has two rows, one has been submitted, the otehr has not.. I ONLY want to see the row that has submitted = 0 (the latest date I'd presume..)
to make matters worse, each action has a revision Id and if the action text is changed, I'm creating a new entry in the action table with the same ID, but a new revision ID.. this however is working great.. but I thought I should mention in case it's interfering with my problem.
Here are my tables and some sample data:
Am I being a monkey?
CREATE TABLE IF NOT EXISTS `action` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_action` int(11) NOT NULL,
`id_priority` int(11) NOT NULL,
`revision` int(11) NOT NULL DEFAULT '1',
`reference` varchar(255) NOT NULL,
`department` int(11) NOT NULL,
`id_parent` int(11) NOT NULL DEFAULT '0',
`sort_order` int(11) NOT NULL,
`description` text NOT NULL,
`date_start` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`date_end` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`date_created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
--
-- Dumping data for table `action`
--
INSERT INTO `action` (`id`, `id_action`, `id_priority`, `revision`, `reference`, `department`, `id_parent`, `sort_order`, `description`, `date_start`, `date_end`, `date_created`) VALUES
(1, 1, 1, 1, '1', 1, 0, 2, 'Test Action revision test 1 a', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '2011-06-17 00:00:00'),
(2, 1, 1, 2, '0', 1, 0, 2, 'Test Action revision test 1 b', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '2011-06-17 00:00:00'),
(3, 2, 1, 1, '0', 1, 0, 1, 'Test Action revision test 2 a', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '2011-06-17 00:00:00'),
(4, 2, 1, 2, '0', 1, 0, 1, 'Test Action revision test 2 b', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '2011-06-17 00:00:00'),
(5, 3, 2, 1, '0', 1, 0, 0, 'Test Action revision test 3 b', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '2011-06-17 00:00:00');
-- --------------------------------------------------------
--
-- Table structure for table `action_status`
--
CREATE TABLE IF NOT EXISTS `action_status` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_department` int(11) NOT NULL,
`id_priority` int(11) NOT NULL,
`id_action` int(11) NOT NULL,
`status` int(11) NOT NULL,
`submitted` tinyint(4) NOT NULL,
`approved` tinyint(4) NOT NULL,
`published` tinyint(4) NOT NULL,
`date_now` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `action_status`
--
INSERT INTO `action_status` (`id`, `id_department`, `id_priority`, `id_action`, `status`, `submitted`, `approved`, `published`, `date_now`) VALUES
(1, 1, 1, 2, 3, 1, 1, 1, '2011-06-20 16:36:09'),
(2, 1, 1, 2, 5, 0, 0, 0, '2011-06-20 16:40:09');
CREATE TABLE IF NOT EXISTS `priority` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`description` text NOT NULL,
PRIMARY KEY (`id`),
KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `priority`
--
INSERT INTO `priority` (`id`, `description`) VALUES
(1, 'Test Priority'),
(2, '2nd Priority');
And my 'problem' SQL
SELECT `action`.`id_priority`, `priority`.`description` as priority, `action`.`reference`, `action`.`description` as action, `action`.`id_action`, `action`.`date_start`, `action`.`date_end`, `action`.`id_parent`, `action_status`.`status`, `action`.`revision`, `action_status`.`submitted`, `action_status`.`date_now`
FROM (`action`)
LEFT JOIN action_status ON
`action_status`.`id_action` = `action`.`id_action`
JOIN `priority` ON
`action`.`id_priority` = `priority`.`id`
WHERE
action.department = 1 AND
action.revision =(SELECT MAX(ar.revision) FROM action as ar WHERE action.id_action = ar.id_action)
GROUP BY `action`.`id_action`
ORDER BY `id_priority` asc, `id_parent` asc, `sort_order` asc
one good recommendation - if you ask for help, simplify your example as much as possible to show the basic root of your problem. No one will try to understand your complex code this way...
What I can't seem to do is list the actions with their latest status value.
You speak about latest status value, so I'd expect that you want the latest record from action_status table, but in your query you check for max revision in action table... And why you group by action_id, when you want to compaI must admit I don't understand the design at all.
Anyway, I think I was solving similar problem few weeks ago, see this MySQL feature reguest ( http://bugs.mysql.com/bug.php?id=2020 ) and my reply at '7 Jul 13:12'. See the following example, it should help you:
Suppose you have table with price for each goods in each store. For each goods, you want to see the minimal price and the related store, in which you get it for the price! Exactly the same as in your example - you want a record with max revision.
create table prices ( goods varchar(10), price double, store varchar(10) );
insert into prices values ('car', 200, 'Amazon'), ('car', 150, 'CarStore'), ('Bread', 2, 'Baker1'), ('Bread', 1, 'Baker2');
select goods, min(price), (select store from prices as p where p.goods = prices.goods
order by price limit 1) as store
from prices
group by goods;
Hope this helps. If you want to pick more columns, just pick an id instead of store and make it as a subquery and join the table again via the id.
If I understood it well and you need only actions with max revision, try something like this:
SELECT *
FROM
(SELECT `action`.`id_action`, max(`action`.`revision`),
(select id
from action as a
where a.id_action = action.id_action
order by revision desc
limit 1) as id_with_max_revision
FROM `action`
WHERE
action.department = 1
GROUP BY `action`.`id_action`
) as action_max_revision
JOIN action on action_max_revision.id_with_max_revision = action.id
LEFT JOIN action_status ON
`action_status`.`id_action` = `action`.`id_action`
JOIN `priority` ON
`action`.`id_priority` = `priority`.`id`
ORDER BY `id_priority` asc, `id_parent` asc, `sort_order` asc
The inner query selects actions with max revision, and the outer query does the other gimcrackery which is not the core of the problem :-)

Dynamic Foreign Keys - How To Implement?

I have 4 tables (appointed, class, elected, status) that I want to cross reference into a single table's (members) column. The values of the of 4 tables are time sensitive based off a history table (members_history). The desired result is that the query should output all members and the current appointed position or current elected position, class, and status within the members row and include additional information obtained from the foreign rows.
So instead of just returning:
id, username, password, salt, name_first, name_last, date_join & date_leave;
The query would return
id, username, password, salt, name_prefix, name_first, name_last, hours_extra, date_join, date_leave, appointed, class, elected & status;
Wherever an added column does not have a current value in history it's result should be NULL.
Now I think I can do this with sub-querys, but have been so far banging my head against the keyboard. I'll take another swing at it later, but until then, anyone else willing to give it a shot, or attempt to point me in the right direction?
The structure of my SQL (no pun intended) tables is as follows:
CREATE TABLE IF NOT EXISTS `members` (
`id` mediumint(3) unsigned NOT NULL auto_increment COMMENT 'Members Unique Id',
`username` varchar(32) collate utf8_bin NOT NULL COMMENT 'Mebers Username',
`password` varchar(64) collate utf8_bin NOT NULL COMMENT 'Members Password Hash',
`salt` varchar(32) collate utf8_bin NOT NULL COMMENT 'Members Password Salt',
`name_first` varchar(32) collate utf8_bin NOT NULL COMMENT 'Members First Name',
`name_last` varchar(32) collate utf8_bin NOT NULL COMMENT 'Members Last Name',
`date_join` date NOT NULL COMMENT 'Members Join Date',
`date_leave` date default NULL COMMENT 'Members Resgination Date (If Applicable)',
PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Members id in this table = mid in other tables';
CREATE TABLE IF NOT EXISTS `members:apointed` (
`id` tinyint(3) unsigned NOT NULL auto_increment COMMENT 'Unique value',
`name_prefix` varchar(8) collate utf8_bin NOT NULL COMMENT 'Prefix Added to Members Name',
`hours_extra` decimal(4,2) NOT NULL COMMENT 'Hours Given as Bonus for Holding this Position.',
`position` varchar(40) collate utf8_bin NOT NULL COMMENT 'Name of the Posisiton',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Undefined within the SOP or By-Laws.';
CREATE TABLE IF NOT EXISTS `members:class` (
`id` tinyint(3) unsigned NOT NULL auto_increment COMMENT 'Unique Id',
`class` varchar(8) collate utf8_bin NOT NULL COMMENT 'Unique Value',
PRIMARY KEY (`id`),
UNIQUE KEY `value` (`class`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Article I, Section 1 Subsection B: Classes of Membership';
CREATE TABLE IF NOT EXISTS `members:elected` (
`id` tinyint(3) unsigned NOT NULL auto_increment COMMENT 'Unique value',
`name_prefix` varchar(8) collate utf8_bin NOT NULL COMMENT 'Prefix Added to Members Name',
`hours_extra` decimal(4,2) NOT NULL COMMENT 'Hours Given as Bonus for Holding this Position.',
`position` varchar(40) collate utf8_bin NOT NULL COMMENT 'Name of the Posisiton',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Article II';
CREATE TABLE IF NOT EXISTS `members:status` (
`id` tinyint(3) unsigned NOT NULL auto_increment COMMENT 'Bit''s Place',
`status` varchar(16) collate utf8_bin NOT NULL COMMENT 'Categorie''s Name',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Article I, Section 1, Subsection A: Categories of Membership';
CREATE TABLE IF NOT EXISTS `members_history` (
`id` int(10) unsigned NOT NULL auto_increment COMMENT 'Unique Id',
`mid` tinyint(3) unsigned NOT NULL COMMENT 'Members Unique Id.',
`table` enum('class','elected','appointed','status') NOT NULL COMMENT 'Name of Table that was Edited.',
`value` tinyint(3) unsigned NOT NULL COMMENT 'Value',
`start` date NOT NULL COMMENT 'Value''s Effect Date',
`end` date default NULL COMMENT 'Value''s Expiration Date',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='Member History';
members_history.mid is a FK for id in the members table, not every member will have history on them (but eventually they all will, as every member will have to have a class and status). members_history.value is a FK for members:{members_history.table}.id;
INSERT INTO `members`
(`id`, `username`, `password`, `salt`, `name_first`, `name_last`, `date_join`, `date_join`) VALUES
( 1, 'Dygear',MD5('pass'), 's417', 'Mark', 'Tomlin', DATE(), NULL),
( 2, 'uberusr',MD5('p455'), '235f', 'Howard', 'Singer', DATE(), NULL),
( 3,'kingchief',MD5('leet'), '32fs','Christopher', 'Buckham', DATE(), NULL);
INSERT INTO `members:apointed`
(`id`, `name_prefix`, `hours_extra`, `posisiton`) VALUES
( 1, '', 0.00, 'Crew Chief'),
( 2, '', 20.00, 'Engineer'),
( 3, 'Lt.', 40.00, 'Lieutenant'),
( 4, 'Capt.', 60.00, 'Captin'),
( 5, 'Chief.', 80.00, '3rd Assistant Chief of Operation');
INSERT INTO `members:class`
(`id`, `class`) VALUES
( 1, 'Class I'),
( 2, 'Class II');
INSERT INTO `members:elected`
(`id`, `name_prefix`, `hours_extra`, `posisiton`) VALUES
( 1, '', 40.00, 'Trustee'),
( 2, '', 40.00, 'Chairman of the Board'),
( 3, 'Prez.', 40.00, 'President'),
( 4, 'VPrez.', 40.00, 'Vice-President'),
( 5, '', 40.00, 'Recording Secretary'),
( 6, '', 40.00, 'Service Secretary'),
( 7, '', 40.00, 'Corresponding Secretary'),
( 8, '', 40.00, 'Financial Secretary Treasuer'),
( 9, '', 40.00, 'Assistant Financial Secretary Treasuer'),
( 10, 'Chief.', 80.00, 'Chief of Operations'),
( 11, 'Chief.', 80.00, 'First Deputy Chief of Operations'),
( 12, 'Chief.', 80.00, 'Second Deputy Chief of Operation');
INSERT INTO `members:status`
(`id`, `status`) VALUES
( 1, 'Active'),
( 2, 'Inactive'),
( 3, 'Student'),
( 4, 'Probationary'),
( 5, 'Lifetime'),
( 6, 'Cadet'),
( 7, 'Honorary'),
( 8, 'Medical'),
( 9, 'Military'),
( 10, 'Resigned'),
( 11, 'Disvowed');
INSERT INTO `members_history`
(`id`, `mid`, `table`, `value`, `start`, `end`) VALUES
(NULL, 1, 'apointed', 3, DATE(), NULL),
(NULL, 1, 'class', 1, DATE(), NULL),
(NULL, 1, 'status', 1, DATE(), NULL),
(NULL, 2, 'elected', 4, DATE(), NULL),
(NULL, 2, 'class', 1, DATE(), NULL),
(NULL, 2, 'status', 1, DATE(), NULL),
(NULL, 3, 'apointed', 10, DATE(), '2010-05-01'),
(NULL, 3, 'class', 1, DATE(), NULL),
(NULL, 3, 'status', 1, DATE(), NULL);
You're using a design called polymorphic associations and it's frequently done wrong. The way to make it work is to create another table, say members:abstract:
CREATE TABLE IF NOT EXISTS `members:abstract` (
`id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`type` enum('class','elected','appointed','status') NOT NULL,
UNIQUE KEY (`id`, `type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
This table serves as the parent table for all of your members attributes tables. Each of these tables changes its primary key to not generate id values automatically, but instead reference the primary key of members:abstract. I'll show just members:appointed but the others would be similar.
CREATE TABLE IF NOT EXISTS `members:appointed` (
`id` INT UNSIGNED NOT NULL PRIMARY KEY, -- not auto_increment
`name_prefix` varchar(8) collate utf8_bin NOT NULL COMMENT 'Prefix Added to Members Name',
`hours_extra` decimal(4,2) NOT NULL COMMENT 'Hours Given as Bonus for Holding this Position.',
`position` varchar(40) collate utf8_bin NOT NULL COMMENT 'Name of the Posisiton',
FOREIGN KEY (`id`) REFERENCES `members:abstract` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Undefined within the SOP or By-Laws.';
You can make this table gain auto-generated values automatically with a trigger:
DELIMITER //
DROP TRIGGER IF EXISTS ins_appointed//
CREATE TRIGGER ins_appointed BEFORE INSERT ON `members:appointed`
FOR EACH ROW BEGIN
INSERT INTO `members:abstract` (`type`) VALUES ('appointed');
SET NEW.id = LAST_INSERT_ID();
END; //
DELIMITER ;
Do the same for each of the other attribute tables.
Note that the id values are now unique across all your attribute tables.
Next you make members:abstract the target for a foreign key in members_history.
CREATE TABLE IF NOT EXISTS `members_history` (
`id` INT unsigned NOT NULL auto_increment COMMENT 'Unique Id',
`mid` INT unsigned NOT NULL COMMENT 'Members Unique Id.',
`value` INT UNSIGNED NOT NULL,
`table` enum('class','elected','appointed','status') NOT NULL,
`start` date NOT NULL COMMENT 'Value''s Effect Date',
`end` date default NULL COMMENT 'Value''s Expiration Date',
PRIMARY KEY (`id`),
FOREIGN KEY (`mid`) REFERENCES `members` (`id`) ON DELETE CASCADE,
FOREIGN KEY (`value`, `table`) REFERENCES `members:abstract` (`id`, `type`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='Member History';
Notice that this table defines a foreign key so that you can't reference an id of the wrong type of attribute in members:abstract.
Now you can rely on referential integrity and consistency which is impossible when you try to implement polymorphic associations without the common parent of all the referenced attribute tables.
Here's the query that returns the result you described (tested on MySQL 5.1.40):
SELECT m.username,
m.password, m.salt, m.name_first, m.name_last,
MAX(a.name_prefix) AS name_prefix,
COALESCE(MAX(a.hours_extra), MAX(e.hours_extra)) AS hours_extra,
MAX(m.date_join) AS date_join,
MAX(m.date_leave) AS date_leave,
MAX(a.position) AS appointed,
MAX(c.class) AS class,
MAX(e.position) AS elected,
MAX(s.status) AS status
FROM `members` m
JOIN `members_history` h ON (h.mid = m.id)
LEFT OUTER JOIN `members:appointed` a ON (h.table = 'appointed' AND h.value = a.id)
LEFT OUTER JOIN `members:class` c ON (h.table = 'class' AND h.value = c.id)
LEFT OUTER JOIN `members:elected` e ON (h.table = 'elected' AND h.value = e.id)
LEFT OUTER JOIN `members:status` s ON (h.table = 'status' AND h.value = s.id)
GROUP BY m.id;
all you need is a left outer join for each of the history types and whatever logic you need to pick the "current" row.
your table structure doesn't quite make enough sense to me to put together a sample for you. maybe if you provide ONE sample member and a couple of rows of history for ONE attribute, i can help you out.