Find item from EAV based on multiple rows - mysql

Basically I am trying to build a 'Search by Attribute' function, so a user can choose a field or combination of fields in an EAV database structure e.g.:
CREATE TABLE `form` (
`formID` int(11) NOT NULL AUTO_INCREMENT,
`formName` varchar(255) NOT NULL,
PRIMARY KEY (`formID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `form` (`formID`, `formName`) VALUES
(1, 'A Form');
CREATE TABLE `formFields` (
`fieldID` int(11) NOT NULL AUTO_INCREMENT,
`formID` int(11) NOT NULL,
`fieldName` varchar(255) NOT NULL,
PRIMARY KEY (`fieldID`),
KEY `formID` (`formID`),
CONSTRAINT `formFields_ibfk_1` FOREIGN KEY (`formID`) REFERENCES `form` (`formID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `formFields` (`fieldID`, `formID`, `fieldName`) VALUES
(1, 1, 'Fruit'),
(2, 1, 'Car Make'),
(3, 1, 'Colour');
CREATE TABLE `item` (
`itemID` int(11) NOT NULL AUTO_INCREMENT,
`formID` int(11) NOT NULL,
`notes` varchar(1000) DEFAULT NULL,
PRIMARY KEY (`itemID`),
KEY `formID` (`formID`),
CONSTRAINT `item_ibfk_1` FOREIGN KEY (`formID`) REFERENCES `form` (`formID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `item` (`itemID`, `formID`, `notes`) VALUES
(1, 1, 'Some notes....'),
(2, 1, 'don\'t find this one');
CREATE TABLE `itemDetails` (
`itemID` int(11) NOT NULL,
`fieldID` int(11) NOT NULL,
`itemValue` varchar(1000) NOT NULL,
UNIQUE KEY `itemID_fieldID` (`itemID`,`fieldID`),
KEY `fieldID` (`fieldID`),
CONSTRAINT `itemDetails_ibfk_1` FOREIGN KEY (`itemID`) REFERENCES `item` (`itemID`),
CONSTRAINT `itemDetails_ibfk_2` FOREIGN KEY (`fieldID`) REFERENCES `formFields` (`fieldID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `itemDetails` (`itemID`, `fieldID`, `itemValue`) VALUES
(1, 1, 'apple'),
(1, 2, 'ford'),
(1, 3, 'orange'),
(2, 1, 'banana'),
(2, 2, 'toyota'),
(2, 3, 'blue');
CREATE OR REPLACE VIEW item_viewDetails AS
SELECT ff.formID, ff.fieldID, id.itemID, ff.fieldName, id.itemValue
FROM formFields ff
LEFT JOIN itemDetails id ON ff.fieldID = id.fieldID;
I have a view set up to return the itemID, formID, fieldID, fieldName and value. each item will have a minimum number of rows based on how many fields are in the form (the value is null if no itemDetails record exists)
currently I have a select statement that can search for 1 field eg: `WHERE view.fieldID = X AND view.value = Y
But I would also like to be able to search for multiple fields at the same time in an AND fashion. but of course i cannot do WHERE (view.fieldID = X AND view.value = Y) AND (view.fieldID = A AND view.value = B) as the details are accross multiple rows with a shared itemID. Is there any way to do this in mysql? or do I have to just OR it and then do checking on the application side to discard invalid results?
I have come up with a solution that does work, but I don't know if it is the best as it involves a sub-query with a view.
SELECT * FROM (
SELECT
COUNT(i.itemID) AS matches,
ia.*
FROM item_viewDetails ia
INNER JOIN item i ON i.itemID = ia.itemID
WHERE (ia.fieldID = 2 AND ia.itemValue = 'toyota') OR (ia.fieldID = 3 AND ia.itemValue = 'blue')
GROUP BY ia.itemID) AS m
WHERE m.matches = 2;
then as I generate the query, I just have to count the number of fields I am trying to match.

Related

Query Data from multiple sql tables

I have an ERD above. I want to get the price of the room based on channel and also the status (isInvisible) of the hotel that owns the room mentioned.
Also a RESTful-API endpoint for that, I tried many times on this assignment and can't get it right as I use Nodejs to write an API GET /api/${roomId}/price but the query doesn't work:
SELECT RoomPrice.price, Status.isInvisible
FROM RoomPrice
INNER JOIN Status
ON (RoomPrice.RoomID = Room.Id AND RoomPrice.ChannelID = ChannelId)
AND (Status.HotelID = Hotels.ID AND Status.ChannelID = ChannelID)
I use query below to create my database in WorkBench:
DROP TABLE IF EXISTS `address`;
CREATE TABLE `address` (
`id` int(9) unsigned NOT NULL AUTO_INCREMENT,
`hotel_id` int(9) unsigned NOT NULL,
`address` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY(hotel_id) REFERENCES hotels(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `address` (`hotel_id`, `address`)
VALUES
(1, '7008 Lynch Centers Apt. 596\nLysannemouth, RI 43355'),
(2, '04795 Stanley Mount Apt. 114\nDorrisborough, DC 38070-3542'),
(1, '24586 Eliseo Haven Suite 045\nKossville, WY 17890-7936'),
(2, '639 Toy Corners\nBashirianfort, CA 08964-7258');
DROP TABLE IF EXISTS `channels`;
CREATE TABLE `channels` (
`id` int(9) unsigned NOT NULL AUTO_INCREMENT,
`url` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
INSERT INTO `channels` (`id`, `url`, `name`)
VALUES
(1, 'http://www.beahan.com/', 'quod'),
(2, 'http://www.douglas.com/', 'sit');
DROP TABLE IF EXISTS `hotels`;
CREATE TABLE `hotels` (
`id` int(9) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
INSERT INTO `hotels` (`id`, `name`)
VALUES
(1, 'illum'),
(2, 'aliquid');
DROP TABLE IF EXISTS `rooms`;
CREATE TABLE `rooms` (
`id` int(9) unsigned NOT NULL AUTO_INCREMENT,
`hotel_id` int(9) unsigned NOT NULL,
`name` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY(hotel_id) REFERENCES hotels(id)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
INSERT INTO `rooms` (`id`, `hotel_id`, `name`)
VALUES
(1, 1, 'vel'),
(2, 2, 'fugit'),
(3, 1, 'doloribus'),
(4, 2, 'ut'),
(5, 1, 'et');
DROP TABLE IF EXISTS `room_prices`;
CREATE TABLE `room_prices` (
`room_id` int(9) unsigned NOT NULL,
`channel_id` int(9) unsigned NOT NULL,
`price` decimal(10,2) NOT NULL,
PRIMARY KEY (room_id, channel_id),
FOREIGN KEY(room_id) REFERENCES rooms(id),
FOREIGN KEY(channel_id) REFERENCES channels(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `room_prices` (`room_id`, `channel_id`, `price`)
VALUES
(1, 1, '50687.86'),
(1, 2, '6687.86'),
(2, 1, '10687.86'),
(2, 2, '274739.20'),
(3, 1, '3828.63'),
(3, 2, '12525.86'),
(4, 1, '2623587.86'),
(4, 2, '125151.00'),
(5, 1, '2358704.85'),
(5, 2, '7347473.86');
DROP TABLE IF EXISTS `status`;
CREATE TABLE `status` (
`hotel_id` int(9) unsigned NOT NULL,
`channel_id` int(9) unsigned NOT NULL,
`isInvisible` BOOLEAN NOT NULL,
PRIMARY KEY (hotel_id, channel_id),
FOREIGN KEY(hotel_id) REFERENCES hotels(id),
FOREIGN KEY(channel_id) REFERENCES channels(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `status` (`hotel_id`, `channel_id`, `isInvisible`)
VALUES
(1, 1, false),
(2, 1, true),
(1, 2, true),
(2, 2, true);
I want that once I query to search for the price of roomId (i.e 1) it returns 2 rows (as the mock data only have 2 rows in channels table) that show:
room_id channel_id price isInvisible
1 1 xxxx.xx 0
1 2 xxxx.xx 1
At this moment, I use the query as DRapp help
SELECT
rp.room_id,
rp.channel_id,
rp.price,
s.isInvisible
FROM
room_prices rp
JOIN status s
ON (rp.channel_id = s.channel_id)
JOIN rooms r
ON (rp.room_id = r.id)
JOIN hotels h
ON (r.hotel_id = h.id)
WHERE rp.room_id = 1
It returns 4 rows (instead of 2 rows as expected)
room_id channel_id price isInvisible
1 1 xxxx.xx 0
1 2 xxxx.xx 1
1 1 xxxx.xx 1
1 2 xxxx.xx 1
You need to identify each of the relations as your ERD shows. Each table is joined to its respective context. Dont jump/skip. The only time you can in this scenario is to skip through the channels since the RoomPrice and Status table EACH have a "ChannelId" to qualify the join
SELECT
rp.price,
s.isInvisible
FROM
Room_Prices rp
JOIN Rooms r
on rp.room_id = r.id
JOIN Status s
ON rp.Channel_ID = s.Channel_Id
AND r.hotel_id = s.hotel_id
WHERE
rp.room_id = 1
I had to revise the query. I looked deeper and noticed your CHANNEL table ALSO had the Hotel, so I had to go from the room prices to the room table. From the room table, I can get the hotel. Now that I have the channel from room prices, AND the hotel ID from the rooms table. So NOW I join to the status table on BOTH columns getting the expected single row per room you are expecting.

how to count same rating from field in sql

I have a problem counting ratings in SQL. This is what my data looks like:
data
CREATE TABLE `restaurant` (
`id_restaurant` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id_restaurant`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
insert into `restaurant`(`id_restaurant`,`name`) values (1,'Mc Donald');
insert into `restaurant`(`id_restaurant`,`name`) values (2,'KFC');
CREATE TABLE `user` (
`id_user` int(11) NOT NULL AUTO_INCREMENT,
`userName` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id_user`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
insert into `user`(`id_user`,`userName`) values (1,'Audey');
CREATE TABLE `factors` (
`factor_id` int(11) NOT NULL AUTO_INCREMENT,
`factor_clean` int(11) NOT NULL DEFAULT '0',
`factor_delicious` int(11) NOT NULL DEFAULT '0',
`id_restaurant` int(11) DEFAULT NULL,
`id_user` int(11) DEFAULT NULL,
PRIMARY KEY (`factor_id`),
KEY `id_restaurant` (`id_restaurant`),
KEY `id_user` (`id_user`),
CONSTRAINT `factors_ibfk_1` FOREIGN KEY (`id_restaurant`) REFERENCES `restaurant` (`id_restaurant`),
CONSTRAINT `factors_ibfk_2` FOREIGN KEY (`id_user`) REFERENCES `user` (`id_user`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
insert into `factors`(`factor_id`,`factor_clean`,`factor_delicious`,`id_restaurant`,`id_user`) values (1,1,5,1,1);
insert into `factors`(`factor_id`,`factor_clean`,`factor_delicious`,`id_restaurant`,`id_user`) values (2,0,5,1,1);
insert into `factors`(`factor_id`,`factor_clean`,`factor_delicious`,`id_restaurant`,`id_user`) values (3,1,5,1,1);
insert into `factors`(`factor_id`,`factor_clean`,`factor_delicious`,`id_restaurant`,`id_user`) values (4,3,3,1,1);
And the result should be like this, Show all ratings (1,2,3,4,5) and their count from the fields rating_clean, rating_delicious, and rating_clean
Thanks for your help.
but the result i get
SELECT COUNT(`factor_clean`+`factor_delicious`),'1' AS rating_1 FROM `factors` WHERE 1 GROUP BY `id_restaurant`
result not should like this
the result should not like that,
my question is, how to select just factor_clean and factor_delicious where factor_clean =1 and factor_delicious = 1
Use union all to unpivot the data and then aggregate:
select id_restaurant, rating, count(*)
from ((select r.id_restaurant, r.rating_clean as rating, r.date
from ratings r
) union all
(select r.id_restaurant, r.rating_delicious, r.date
from ratings r
) union all
(select r.id_restaurant, r.rating_clean2, r.date
from ratings r
)
) r
group by id_restaurant, rating
order by id_restaurant, rating;
For example this is solution for table with colums rating_delicious and rating_clean (only one!):
First of all you should create additional table, I called it factors:
CREATE TABLE `factors` (
`factor_id` int(11) NOT NULL AUTO_INCREMENT,
`factor_clean` int(11) NOT NULL DEFAULT '0',
`factor_delicious` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`factor_id`)
)
Next add two records:
INSERT INTO `factors` (`factor_id`, `factor_clean`, `factor_delicious`) VALUES (NULL, '1', '0'), (NULL, '0', '1');
Now you can join this tables and get results:
SELECT x.id_restaurant
, (x.rating_clean * f.factor_clean) + (x.rating_delicious * f.factor_delicious) AS rating
, count(*)
FROM your_table x
JOIN factors f
WHERE 1
GROUP
BY x.id_restaurant
, rating
In order to use next colum (rating_third), you should and column factor_third to factors, insert new row with 1 in this column and finally add something like your_table.rating_third*factors.factor_third to sum in SELECT

MySQL joining 2 tables in to one main table

I have 2 tables 'cat' and 'sub_cat' and these two tables should join or something with main table 'product'
I tried all joining methods and non of them gave me right result I want.
I'm sure there is a method. I don't know what to call.
Sample SQL
This is how the last query should be
forget about the normalization theories and every thing and I just want the last query to be like this or mysql method that I can use on this.
cat_id cannot be duplicate
s_id should also cannot be duplicate
like in third row there can be: cat_id but no s_id the s_id should be null
if there is no cat_id and no s_id both should be null like fourth row
p_id can be duplicate
can't use group by or distinct cause it doesn't give null values then as i know
only method i got closer is using left joining both cat and sub_cat to prod table but it gives me duplicate cat_id and s_id and can't use group by or distinct on this cause there should be null values.
here is the test data
CREATE TABLE IF NOT EXISTS `cat` (
`product_id` int(11) DEFAULT NULL,
`cat_id` int(11) DEFAULT NULL,
`cat_name` varchar(10) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `cat` (`product_id`, `cat_id`, `cat_name`) VALUES
(1, 1, 'cat1'),
(2, 2, 'cat2'),
(3, 3, 'cat3'),
(1, 4, 'ca4');
CREATE TABLE IF NOT EXISTS `prod` (
`product_id` int(11) DEFAULT NULL,
`name` varchar(10) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `prod` (`product_id`, `name`) VALUES
(1, 'prod1'),
(2, 'prod2'),
(3, 'pro3'),
(4, 'prod4');
CREATE TABLE IF NOT EXISTS `sub_cat` (
`product_id` int(11) DEFAULT NULL,
`sub_cat_id` int(11) DEFAULT NULL,
`sub_cat_name` varchar(10) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `sub_cat` (`product_id`, `sub_cat_id`, `sub_cat_name`) VALUES
(1, 1, 'sub cat 1'),
(2, 2, 'sub cat 2'),
(1, 3, 'sub3');
I have done a similar thing in this one.prop_cat acts as you Category table,prop_subcat as your subcategory table and property as you product.
CREATE TABLE `prop_cat` (
`pcat_id` int(11) NOT NULL AUTO_INCREMENT,
`pcat_name` varchar(60) DEFAULT NULL,
PRIMARY KEY (`pcat_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
CREATE TABLE `prop_subcat` (
`psubcat_id` int(11) NOT NULL,
`pcat_id` int(11) NOT NULL,
`psubcat_name` varchar(45) DEFAULT NULL,
PRIMARY KEY (`psubcat_id`,`pcat_id`),
KEY `pspc_idx` (`pcat_id`),
CONSTRAINT `catsub` FOREIGN KEY (`pcat_id`) REFERENCES `prop_cat` (`pcat_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `property` (
`prop_id` int(11) NOT NULL AUTO_INCREMENT,
`prop_name` varchar(25) DEFAULT NULL,
`price` double DEFAULT NULL,
`location` varchar(50) DEFAULT NULL,
`image` varchar(60) DEFAULT NULL,
`area` double DEFAULT NULL,
`psubcat_id` int(11) DEFAULT NULL,
`description` text,
PRIMARY KEY (`prop_id`),
KEY `psub_idx` (`psubcat_id`),
CONSTRAINT `psub` FOREIGN KEY (`psubcat_id`) REFERENCES `prop_subcat` (`pcat_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
SELECT
prop_cat.`pcat_id` AS prop_cat_pcat_id,
prop_cat.`pcat_name` AS prop_cat_pcat_name,
prop_subcat.`psubcat_id` AS prop_subcat_psubcat_id,
prop_subcat.`pcat_id` AS prop_subcat_pcat_id,
prop_subcat.`psubcat_name` AS prop_subcat_psubcat_name,
property.`prop_id` AS property_prop_id,
property.`prop_name` AS property_prop_name,
property.`price` AS property_price,
property.`location` AS property_location,
property.`image` AS property_image,
property.`area` AS property_area,
property.`psubcat_id` AS property_psubcat_id,
property.`description` AS property_description
FROM
`prop_cat` prop_cat INNER JOIN `prop_subcat` prop_subcat ON prop_cat.`pcat_id` = prop_subcat.`pcat_id`
INNER JOIN `property` property ON prop_subcat.`pcat_id` = property.`psubcat_id`

Mysql - Help me alter this query to apply AND logic instead of OR in searching?

First execute these tables and data dumps :-
CREATE TABLE IF NOT EXISTS `Tags` (
`id_tag` int(10) unsigned NOT NULL auto_increment,
`tag` varchar(255) default NULL,
PRIMARY KEY (`id_tag`),
UNIQUE KEY `tag` (`tag`),
KEY `id_tag` (`id_tag`),
KEY `tag_2` (`tag`),
KEY `tag_3` (`tag`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;
INSERT INTO `Tags` (`id_tag`, `tag`) VALUES
(1, 'key1'),
(2, 'key2');
CREATE TABLE IF NOT EXISTS `Tutors_Tag_Relations` (
`id_tag` int(10) unsigned NOT NULL default '0',
`id_tutor` int(10) default NULL,
KEY `Tutors_Tag_Relations` (`id_tag`),
KEY `id_tutor` (`id_tutor`),
KEY `id_tag` (`id_tag`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `Tutors_Tag_Relations` (`id_tag`, `id_tutor`) VALUES
(1, 1),
(2, 1);
The following query finds all the tutors from Tutors_Tag_Relations table which have reference to at least one of the terms "key1" or "key2".
SELECT td . *
FROM Tutors_Tag_Relations AS td
INNER JOIN Tags AS t ON t.id_tag = td.id_tag
WHERE t.tag LIKE "%key1%"
OR t.tag LIKE "%key2%"
Group by td.id_tutor
LIMIT 10
Please help me modify this query so that it returns all the tutors from Tutors_Tag_Relations table which have reference to both the terms "key1" and "key2" (AND logic instead of OR logic). Please suggest an optimized query considering huge number of data records (the query should NOT individually fetch two sets of tutors matching each keyword and then find the intersection).
Update
Taking the question to the next level. Please run the following fresh queries :-
===================================================================================
CREATE TABLE IF NOT EXISTS learning_packs_tag_relations (
id_tag int(10) unsigned NOT NULL DEFAULT '0',
id_tutor int(10) DEFAULT NULL,
id_lp int(10) unsigned DEFAULT NULL,
KEY Learning_Packs_Tag_Relations_FKIndex1 (id_tag),
KEY id_lp (id_lp),
KEY id_tag (id_tag)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS learning_packs (
id_lp int(10) unsigned NOT NULL AUTO_INCREMENT,
id_status int(10) unsigned NOT NULL DEFAULT '2',
id_author int(10) unsigned NOT NULL DEFAULT '0',
name varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (id_lp)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=21 ;
CREATE TABLE IF NOT EXISTS tutors_tag_relations (
id_tag int(10) unsigned NOT NULL DEFAULT '0',
id_tutor int(10) DEFAULT NULL,
KEY Tutors_Tag_Relations (id_tag),
KEY id_tutor (id_tutor),
KEY id_tag (id_tag)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS users (
id_user int(10) unsigned NOT NULL AUTO_INCREMENT,
name varchar(100) NOT NULL DEFAULT '',
surname varchar(155) NOT NULL DEFAULT '',
PRIMARY KEY (id_user)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=52 ;
CREATE TABLE IF NOT EXISTS tutor_details (
id_tutor int(10) NOT NULL AUTO_INCREMENT,
id_user int(10) NOT NULL,
PRIMARY KEY (id_tutor)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=60 ;
CREATE TABLE IF NOT EXISTS tags (
id_tag int(10) unsigned NOT NULL AUTO_INCREMENT,
tag varchar(255) DEFAULT NULL,
PRIMARY KEY (id_tag),
UNIQUE KEY tag (tag)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
ALTER TABLE learning_packs_tag_relations
ADD CONSTRAINT Learning_Packs_Tag_Relations_ibfk_1 FOREIGN KEY (id_tag) REFERENCES tags (id_tag) ON DELETE NO ACTION ON UPDATE NO ACTION;
ALTER TABLE learning_packs
ADD CONSTRAINT Learning_Packs_ibfk_2 FOREIGN KEY (id_author) REFERENCES users (id_user) ON DELETE NO ACTION ON UPDATE NO ACTION;
ALTER TABLE tutors_tag_relations
ADD CONSTRAINT Tutors_Tag_Relations_ibfk_1 FOREIGN KEY (id_tag) REFERENCES tags (id_tag) ON DELETE NO ACTION ON UPDATE NO ACTION;
INSERT INTO test.users (
id_user ,
name ,
surname
)
VALUES (
NULL , 'Vivian', 'Richards'
), (
NULL , 'Sachin', 'Tendulkar'
);
INSERT INTO test.users (
id_user ,
name ,
surname
)
VALUES (
NULL , 'Don', 'Bradman'
);
INSERT INTO test.tutor_details (
id_tutor ,
id_user
)
VALUES (
NULL , '52'
), (
NULL , '53'
);
INSERT INTO test.tutor_details (
id_tutor ,
id_user
)
VALUES (
NULL , '54'
);
INSERT INTO test.tags (
id_tag ,
tag
)
VALUES (
1 , 'Vivian'
), (
2 , 'Richards'
);
INSERT INTO test.tags (id_tag, tag) VALUES (3, 'Sachin'), (4, 'Tendulkar');
INSERT INTO test.tags (id_tag, tag) VALUES (5, 'Don'), (6, 'Bradman');
INSERT INTO test.learning_packs (id_lp, id_status, id_author, name) VALUES ('1', '1', '52', 'Cricket 1'), ('2', '2', '52', 'Cricket 2');
INSERT INTO test.tags (id_tag, tag) VALUES ('7', 'Cricket'), ('8', '1');
INSERT INTO test.tags (id_tag, tag) VALUES ('9', '2');
INSERT INTO test.learning_packs_tag_relations (id_tag, id_tutor, id_lp) VALUES ('7', '52', '1'), ('8', '52', '1');
INSERT INTO test.learning_packs_tag_relations (id_tag, id_tutor, id_lp) VALUES ('7', '52', '2'), ('9', '52', '2');
===================================================================================
About the new system -
- The system now has 4 more tables - tutors, Users (linked to tutor_details), learning_packs, learning_packs_tag_relations
- Tutors create packs - tag relations for tutors stored in tutors_tag_relations and those for packs stored in learning_packs_tag_relations.
Now I want to search learning_packs, with the same AND logic. Help me modify the following query so that searching pack name or tutor's name, surname results all active packs (either directly those packs or packs created by those tutors).
==================================================================================
select lp.*
from Learning_Packs AS lp
LEFT JOIN Learning_Packs_Tag_Relations AS lptagrels ON lp.id_lp = lptagrels.id_lp
LEFT JOIN Tutors_Tag_Relations as ttagrels ON lp.id_author = ttagrels.id_tutor
LEFT JOIN Tutor_Details AS td ON ttagrels.id_tutor = td.id_tutor
LEFT JOIN Users as u on td.id_user = u.id_user
JOIN Tags as t on (t.id_tag = lptagrels.id_tag) or (t.id_tag = ttagrels.id_tag)
where lp.id_status = 1 AND ( t.tag LIKE "%Vivian%" OR t.tag LIKE "%Richards%" )
group by lp.id_lp HAVING count(lp.id_lp) > 1 limit 0,20
As you can see, searching "Cricket 1" returns that pack but searching Vivian Richards does not return the same pack.
Please help
Pretty simple if using Group and Having. This should get what you are looking for.
SELECT id_tutor
FROM Tutors_Tag_Relations AS td
INNER JOIN Tags AS t ON t.id_tag = td.id_tag
WHERE t.tag LIKE "%key1%"
or t.tag LIKE "%key2%"
group by id_tutor
having count(id_tutor)>1

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.