MYSQL Double Counting Sum - mysql

I am trying to sum the views from the impression table but the answer of some are doubling because there are more than 1 flight for some of the campaigns.
I am combining the data from 3 different tables.
Is there something I can do so only one row from the flight table is returned?
SELECT CAMPAIGN_CPM, CAMPAIGN.CAMPAIGN_ID, CAMPAIGN_NAME, MONTHNAME(IMP_DATE), SUM(VIEWS), CONCAT(MONTH(IMP_DATE),'-',YEAR(IMP_DATE)) AS MonthYear
FROM IMPRESSION
INNER JOIN CAMPAIGN
ON IMPRESSION.CAMPAIGN_ID = CAMPAIGN.CAMPAIGN_ID
INNER JOIN FLIGHT
ON FLIGHT.CAMPAIGN_ID = CAMPAIGN.CAMPAIGN_ID
WHERE MONTH(IMP_DATE) = MONTH(now() - interval 1 MONTH) AND CAMPAIGN_CPM > 0
GROUP BY MONTHNAME(IMP_DATE), IMPRESSION.CAMPAIGN_ID, CAMPAIGN_CPM;
If any other information is need please let me know.
Tables
CREATE TABLE `FLIGHT` (
`FLIGHT_ID` int(11) NOT NULL,
`FLIGHT_NAME` varchar(255) DEFAULT NULL,
`FLIGHT_START_DATE` date DEFAULT NULL,
`FLIGHT_END_DATE` date DEFAULT NULL,
`CAMPAIGN_FREQUENCY` int(11) DEFAULT NULL,
`CAMPAIGN_FREQUENCY_PERIOD` varchar(255) DEFAULT NULL,
`CAMPAIGN_CPM` double DEFAULT NULL,
`CAMPAIGN_STATUS` varchar(255) DEFAULT NULL,
`CUSTOM_CAMPAIGN_TYPE` varchar(255) DEFAULT NULL,
`CAMPAIGN_ID` int(11) NOT NULL,
PRIMARY KEY (`FLIGHT_ID`),
KEY `CAMPAIGN` (`CAMPAIGN_ID`),
CONSTRAINT `CAMPAIGN_FK4` FOREIGN KEY (`CAMPAIGN_ID`) REFERENCES `CAMPAIGN` (`CAMPAIGN_ID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `IMPRESSION` (
`IMP_DATE` date NOT NULL,
`CAMPAIGN_ID` int(11) NOT NULL,
`BANNER_ID` int(11) DEFAULT NULL,
`CUSTOMER_ID` int(11) DEFAULT NULL,
`ADVERTISER_ID` int(11) DEFAULT NULL,
`PLACEMENT_ID` int(11) DEFAULT NULL,
`IMPRESSIONS_WITH_DEFAULTS` int(11) DEFAULT NULL,
`IMPRESSIONS_WITHOUT_DEFAULTS` int(11) DEFAULT NULL,
`VIEWS` int(11) DEFAULT NULL,
`DEFAULTS` int(11) DEFAULT NULL,
`CLICKS` int(11) DEFAULT NULL,
`IMPRESSION_ID` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`IMPRESSION_ID`),
KEY `CAMPAIGN_ID` (`CAMPAIGN_ID`),
KEY `BANNER_ID` (`BANNER_ID`),
KEY `CUSTOMER_ID` (`CUSTOMER_ID`),
KEY `ADVERTISER_ID` (`ADVERTISER_ID`),
KEY `PLACEMENT_ID` (`PLACEMENT_ID`),
KEY `CAMPAIGN_ID_2` (`CAMPAIGN_ID`),
CONSTRAINT `DSF` FOREIGN KEY (`PLACEMENT_ID`) REFERENCES `PLACEMENT` (`PLACEMENT_ID`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `ADVERTISER_FK9` FOREIGN KEY (`ADVERTISER_ID`) REFERENCES `ADVERTISER` (`ADVERTISER_ID`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `BANNER_FK5` FOREIGN KEY (`BANNER_ID`) REFERENCES `BANNER` (`BANNER_ID`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `CAMPAIGN_FK3` FOREIGN KEY (`CAMPAIGN_ID`) REFERENCES `CAMPAIGN` (`CAMPAIGN_ID`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `CUSTOMER_FK5` FOREIGN KEY (`CUSTOMER_ID`) REFERENCES `CUSTOMER` (`CUSTOMER_ID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=35673 DEFAULT CHARSET=utf8
CREATE TABLE `CAMPAIGN` (
`CAMPAIGN_ID` int(11) NOT NULL,
`CAMPAIGN_NAME` varchar(255) DEFAULT NULL,
`CAMPAIGN_START_DATE` date DEFAULT NULL,
`CAMPAIGN_END_DATE` date DEFAULT NULL,
`ADVERTISER_ID` int(11) NOT NULL,
PRIMARY KEY (`CAMPAIGN_ID`),
KEY `ADVERTISER` (`ADVERTISER_ID`),
CONSTRAINT `ADVERTISER_FK2` FOREIGN KEY (`ADVERTISER_ID`) REFERENCES `ADVERTISER` (`ADVERTISER_ID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Thanks in advance.

Try this:
I have added FLIGHT to show that one flight number is shown and I changed the location from views.
SQL code:
SELECT * FROM (
SELECT FLIGHT.FLIGHT_ID, SUM(VIEWS), CAMPAIGN_CPM, CAMPAIGN.CAMPAIGN_ID, CAMPAIGN_NAME, MONTHNAME(IMP_DATE), CONCAT(MONTH(IMP_DATE),'-',YEAR(IMP_DATE)) AS MonthYear
FROM IMPRESSION
INNER JOIN CAMPAIGN
ON IMPRESSION.CAMPAIGN_ID = CAMPAIGN.CAMPAIGN_ID
INNER JOIN FLIGHT
ON FLIGHT.CAMPAIGN_ID = CAMPAIGN.CAMPAIGN_ID
WHERE MONTH(IMP_DATE) = MONTH(now() - interval 1 MONTH) AND CAMPAIGN_CPM > 0
GROUP BY MONTHNAME(IMP_DATE), IMPRESSION.CAMPAIGN_ID, CAMPAIGN_CPM
) a GROUP BY a.CAMPAIGN_ID;
I have made some small alterations because I had to fill the tables with sample data. Furthermore, the there were some issues with the primary keys and cascading. However, Every should work in your version.
TABLE structure:
CREATE TABLE `FLIGHT` (
`FLIGHT_ID` int(11) NOT NULL PRIMARY KEY,
`FLIGHT_NAME` varchar(255) DEFAULT NULL,
`FLIGHT_START_DATE` date DEFAULT NULL,
`FLIGHT_END_DATE` date DEFAULT NULL,
`CAMPAIGN_FREQUENCY` int(11) DEFAULT NULL,
`CAMPAIGN_FREQUENCY_PERIOD` varchar(255) DEFAULT NULL,
`CAMPAIGN_CPM` double DEFAULT NULL,
`CAMPAIGN_STATUS` varchar(255) DEFAULT NULL,
`CUSTOM_CAMPAIGN_TYPE` varchar(255) DEFAULT NULL,
`CAMPAIGN_ID` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `IMPRESSION` (
`IMP_DATE` date NOT NULL,
`CAMPAIGN_ID` int(11) NOT NULL,
`BANNER_ID` int(11) DEFAULT NULL,
`CUSTOMER_ID` int(11) DEFAULT NULL,
`ADVERTISER_ID` int(11) DEFAULT NULL,
`PLACEMENT_ID` int(11) DEFAULT NULL,
`IMPRESSIONS_WITH_DEFAULTS` int(11) DEFAULT NULL,
`IMPRESSIONS_WITHOUT_DEFAULTS` int(11) DEFAULT NULL,
`VIEWS` int(11) DEFAULT NULL,
`DEFAULTS` int(11) DEFAULT NULL,
`CLICKS` int(11) DEFAULT NULL,
`IMPRESSION_ID` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY
) ENGINE=InnoDB AUTO_INCREMENT=35673 DEFAULT CHARSET=utf8;
CREATE TABLE `CAMPAIGN` (
`CAMPAIGN_ID` int(11) NOT NULL PRIMARY KEY,
`CAMPAIGN_NAME` varchar(255) DEFAULT NULL,
`CAMPAIGN_START_DATE` date DEFAULT NULL,
`CAMPAIGN_END_DATE` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
SAMPLE Data:
INSERT INTO FLIGHT (FLIGHT_ID, CAMPAIGN_ID,CAMPAIGN_CPM)
VALUES (255060,1,250),
(255080,1,250),
(255070,1,250),
(255020,1,40),
(255025,2,300),
(255040,3,250),
(255045,3,20),
(255046,3,100),
(255071,4,210),
(255055,5,280);
INSERT INTO IMPRESSION (IMP_DATE,CAMPAIGN_ID, VIEWS)
VALUES
((NOW() - INTERVAL 10 DAY),1,250),
((NOW() - INTERVAL 1 MONTH),2,4050),
((NOW() - INTERVAL 1 MONTH),3,10),
((NOW() - INTERVAL 3 DAY),3,40),
((NOW() - INTERVAL 1 MONTH),3,10),
((NOW() - INTERVAL 3 DAY),4,40),
((NOW() - INTERVAL 1 MONTH),5,1350);
INSERT INTO CAMPAIGN (CAMPAIGN_ID, CAMPAIGN_NAME)
VALUES
(1,'jkajoiuwejoijdkjjklja'),
(2,'ajiuiouwoihajdhjhhjh'),
(3, 'aauuehxbhchuhnb'),
(4,'aduuiuhzjcnckjnxkj'),
(5,'zzzdfhjfhajkhwkjeajqw');
Here is a SQLFIDDLE demo. Try it out. Good luck with you project.

Related

What is the better way to select minimum value within join?

I've three tables where I want to select the oldest media according to date from a joined table. The query is working fine but I want to know if its correct solution for performance or there is a better way to do this.
SELECT product.`product_id`, product.`title`, product.`price`,
media_gallery.`thumbnail`, product.`special_price`
FROM product
LEFT JOIN
(SELECT product_media.`product_id`, product_media.`media_id`
FROM product_media ORDER BY product_media.`added_on` ASC LIMIT 1) product_media
ON product.`product_id` = product_media.`product_id`
LEFT JOIN media_gallery ON product_media.`media_id` = media_gallery.`media_id`
GROUP BY product.`product_id`
LIMIT 10
I've three tables from which I want to select the products with single image and the image should be the oldest. The image is stored in media_gallery and the image is assigned in product_media. The product can have multiple images which are referenced in product_media. I want to select product_id, product.title, product.price, product.special_price, media_gallery.thumbnail. The thumbnail should be the image added as earliest
CREATE TABLE `product_media` (
`id` varchar(60) NOT NULL,
`product_id` varchar(50) NOT NULL,
`media_id` varchar(50) NOT NULL,
`added_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `product_id_2` (`product_id`,`media_id`),
KEY `product_id` (`product_id`),
KEY `media_id` (`media_id`),
CONSTRAINT `product_media_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `product` (`product_id`) ON DELETE CASCADE ON UPDATE NO ACTION,
CONSTRAINT `product_media_ibfk_2` FOREIGN KEY (`media_id`) REFERENCES `media_gallery` (`media_id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `media_gallery` (
`media_id` varchar(50) NOT NULL,
`media_type` varchar(10) NOT NULL,
`media_title` varchar(100) DEFAULT NULL,
`media_url` varchar(255) NOT NULL,
`thumbnail` varchar(255) NOT NULL,
`added_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`media_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `product` (
`product_id` varchar(50) NOT NULL,
`title` varchar(255) NOT NULL,
`description` varchar(2000) DEFAULT NULL,
`price` decimal(10,2) NOT NULL,
`special_price` decimal(10,2) DEFAULT NULL,
`status` varchar(15) DEFAULT 'AVAILABLE',
`weight` double DEFAULT NULL,
`unit_id` varchar(50) DEFAULT NULL,
`added_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`product_id`),
UNIQUE KEY `sku` (`sku`),
KEY `unit_id` (`unit_id`),
CONSTRAINT `product_ibfk_1` FOREIGN KEY (`unit_id`) REFERENCES `units` (`unit_id`) ON DELETE SET NULL ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Unable to update a child row in my Database (MySQL)

Using the following SQL statement..
INSERT INTO `debates` (`id`, `unit_id`, `starter_pack_id`, `academic_id`, `title`, `date_due`, `date_created`, `date_modified`) VALUES
(1, 1, 1, 5, 'On Campus', '2016-12-25 00:00:00', now(), NULL),
(2, 1, 1, 5, 'Off Campus', '2016-12-25 00:00:00', now(), NULL);
Results in the following error being output..
#1452 - Cannot add or update a child row: a foreign key constraint fails (`my_database`.`debates`, CONSTRAINT `fk_debates_starter_pack_id` FOREIGN KEY (`starter_pack_id`) REFERENCES `debate_starter_packs` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
Why am I unable to update this table?
Please Note : Below are all the tables associated with this SQL statement. There are currently no tables contain any data in the database.
Debates:
CREATE TABLE `debates`(
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`unit_id` INT(11) UNSIGNED NOT NULL,
`starter_pack_id` TINYINT(3) UNSIGNED NOT NULL,
`academic_id` INT(11) UNSIGNED NOT NULL,
`title` VARCHAR(255) NOT NULL,
`date_due` DATETIME NOT NULL,
`date_created` DATETIME NOT NULL,
`date_modified` DATETIME DEFAULT NULL,
CONSTRAINT `pk_debates_id` PRIMARY KEY(`id`),
CONSTRAINT `fk_debates_starter_pack_id` FOREIGN KEY(`starter_pack_id`) REFERENCES `debate_starter_packs`(`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_debates_unit_id` FOREIGN KEY(`unit_id`) REFERENCES `units`(`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_debates_user_role_id` FOREIGN KEY(`academic_id`) REFERENCES `user_roles`(`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8;
Units (Unit_ID Reference):
CREATE TABLE `units` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`academic_id` int(11) UNSIGNED NOT NULL,
`unit_code` varchar(6) NOT NULL,
`title` varchar(100) DEFAULT NULL,
`date_added` datetime NOT NULL,
`date_modified` datetime DEFAULT NULL,
CONSTRAINT `pk_units_id` PRIMARY KEY(`id`),
CONSTRAINT `fk_units_academic_id` FOREIGN KEY (`academic_id`) REFERENCES `user_roles` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Starter Packs (Starter_Pack_ID Reference):
CREATE TABLE `debate_starter_packs` (
`id` tinyint(3) UNSIGNED NOT NULL AUTO_INCREMENT,
`academic_id` int(11) UNSIGNED NOT NULL,
`title` varchar(100) NOT NULL,
`description` text NOT NULL,
`date_created` datetime NOT NULL,
`date_modified` datetime DEFAULT NULL,
CONSTRAINT `pk_debate_starter_packs_id` PRIMARY KEY(`id`),
CONSTRAINT `fk_debate_starter_packs_user_role_id` FOREIGN KEY (`academic_id`) REFERENCES `user_roles` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Users (Academic_ID Reference):
CREATE TABLE `users` (
`id` int(11) UNSIGNED NOT NULL,
`username` varchar(100) DEFAULT NULL,
`first_name` varchar(50) DEFAULT NULL,
`last_name` varchar(50) DEFAULT NULL,
`email` varchar(100) NOT NULL,
`password` varchar(255) NOT NULL,
`salt` varchar(255) DEFAULT NULL,
`organisation` varchar(100) DEFAULT NULL,
`phone` varchar(20) DEFAULT NULL,
`date_created` datetime NOT NULL,
`date_last_login` datetime DEFAULT NULL,
`remember_code` varchar(40) DEFAULT NULL,
`active` tinyint(1) UNSIGNED NOT NULL,
CONSTRAINT `pk_users_id` PRIMARY KEY(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Do you have the record id='1' in the table debate_starter_packs ?
The table debates have a foreign key reference id of table debate_starter_packs.
CONSTRAINT `fk_debates_starter_pack_id` FOREIGN KEY(`starter_pack_id`) REFERENCES `debate_starter_packs`(`id`) ON DELETE CASCADE ON UPDATE CASCADE,
You should insert the record id='1' to the table debate_starter_packs first.

sum function with join between two date ranges

hi friends i have two tables with name order and product order
CREATE TABLE `order` (
`order_num` int(11) NOT NULL AUTO_INCREMENT,
`date` date DEFAULT NULL,
`time` time DEFAULT NULL,
`cutomer_name` varchar(45) DEFAULT NULL,
PRIMARY KEY (`order_num`)
) ENGINE=InnoDB AUTO_INCREMENT=235 DEFAULT CHARSET=latin1
And
CREATE TABLE `product_order` (
`order_num` int(11) NOT NULL,
`idproduct` int(11) NOT NULL,
`quantity` tinyint(4) DEFAULT NULL,
`sub_price` int(11) DEFAULT NULL,
`price` int(11) DEFAULT NULL,
`actual_price` int(11) DEFAULT NULL,
PRIMARY KEY (`order_num`,`idproduct`),
KEY `fk_product_order_order1_idx` (`order_num`),
KEY `fk_product_order_product1_idx` (`idproduct`),
CONSTRAINT `fk_product_order_order1` FOREIGN KEY (`order_num`) REFERENCES `order` (`order_num`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_product_order_product1` FOREIGN KEY (`idproduct`) REFERENCES `product` (`idproduct`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
i want to join both table and want to retrieve the sum of product_order.actual_price,product_order.sub_price and product_order.quantity
between specific dates
my query return zero rows and i don't know how to solve this problem
here is my query
Select `order`.date,
Sum(product_order.actual_price) as actual_price_sum,
Sum(product_order.sub_price) as sub_price_sum,
Sum(product_order.quantity) as sold_quantity,
(Sum(product_order.sub_price)-Sum(product_order.actual_price)) as profit
From `order`
Inner Join product_order On `order`.order_num = product_order.order_num
WHERE CAST(`date` AS date) BETWEEN '2014-11-11' and '2015-11-11'
Group By `order`.date

Cannot delete foreign key constraint fails

When I try to delete the question from the test, the sql show :
`Cannot delete or update a parent row: a foreign key constraint fails (`oes`.`studentquestion`,
CONSTRAINT `studentquestion_ibfk_2` FOREIGN KEY (`testid`, `qnid`) REFERENCES `question`
(`testid`, `qnid`))`
Here will be my table:
student table
CREATE TABLE `student` (
`stdid` bigint(20) NOT NULL,
`subid` int(11) NOT NULL,
`stdname` varchar(40) default NULL,
`stdpassword` varchar(40) default NULL,
`emailid` varchar(40) default NULL,
`contactno` varchar(20) default NULL,
`address` varchar(40) default NULL,
`city` varchar(40) default NULL,
`pincode` varchar(20) default NULL,
PRIMARY KEY (`stdid`),
UNIQUE KEY `stdname` (`stdname`),
UNIQUE KEY `emailid` (`emailid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
studentquestion table
CREATE TABLE `studentquestion` (
`stdid` bigint(20) NOT NULL default '0',
`testid` bigint(20) NOT NULL default '0',
`qnid` int(11) NOT NULL default '0',
`answered` enum('answered','unanswered','review') DEFAULT NULL,
`stdanswer` enum('optiona','optionb','optionc','optiond') DEFAULT NULL,
PRIMARY KEY (`stdid`,`testid`,`qnid`),
KEY `testid` (`testid`,`qnid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
studenttest table
`stdid` bigint(20) NOT NULL default '0',
`testid` bigint(20) NOT NULL default '0',
`starttime` timestamp NOT NULL default CURRENT_TIMESTAMP,
`endtime` timestamp NOT NULL default '0000-00-00 00:00:00',
`correctlyanswered` int(11) default NULL,
`status` enum('over','inprogress') default NULL,
PRIMARY KEY (`stdid`,`testid`),
KEY `testid` (`testid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
question table
CREATE TABLE `question` (
`testid` bigint(20) NOT NULL default '0',
`qnid` int(11) NOT NULL default '0',
`question` varchar(500) default NULL,
`optiona` varchar(100) DEFAULT NULL,
`optionb` varchar(100) DEFAULT NULL,
`optionc` varchar(100) DEFAULT NULL,
`optiond` varchar(100) DEFAULT NULL,
`correctanswer` enum ('optiona','optionb','optionc','optiond') DEFAULT NULL,
`marks` int(11) DEFAULT NULL,
PRIMARY KEY (`testid`,`qnid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
test table
CREATE TABLE `test` (
`testid` bigint(20) NOT NULL,
`testname` varchar(30) NOT NULL,
`testdesc` varchar(100) default NULL,
`testdate` date default NULL,
`testtime` time default NULL,
`subid` int(11) default NULL,
`testfrom` timestamp NOT NULL default CURRENT_TIMESTAMP,
`testto` timestamp NOT NULL default '0000-00-00 00:00:00',
`duration` int(11) default NULL,
`totalquestions` int(11) default NULL,
`attemptedstudents` bigint(20) DEFAULT NULL,
`testcode` varchar(40) NOT NULL,
`tcid` bigint(20) default NULL,
`minimunscore` int(11) NOT NULL,
PRIMARY KEY (`testid`),
UNIQUE KEY `testname` (`testname`),
KEY `test_fk1` (`subid`),
KEY `test_fk2` (`tcid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
subject table
CREATE TABLE `subject` (
`subid` int(11) NOT NULL,
`subname` varchar(40) default NULL,
`subdesc` varchar(100) default NULL,
`tcid` bigint(20) default NULL,
PRIMARY KEY (`subid`),
UNIQUE KEY `subname` (`subname`),
KEY `subject_fk1` (`tcid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `studentquestion`
ADD CONSTRAINT `studentquestion_ibfk_1` FOREIGN KEY (`stdid`) REFERENCES `student`
(`stdid`),
ADD CONSTRAINT `studentquestion_ibfk_2` FOREIGN KEY (`testid`, `qnid`) REFERENCES `question`
(`testid`, `qnid`);
ALTER TABLE `studenttest`
ADD CONSTRAINT `studenttest_ibfk_1` FOREIGN KEY (`stdid`) REFERENCES `student` (`stdid`),
ADD CONSTRAINT `studenttest_ibfk_2` FOREIGN KEY (`testid`) REFERENCES `test` (`testid`);
ALTER TABLE `question`
ADD CONSTRAINT `question_ibfk_1` FOREIGN KEY (`testid`) REFERENCES `test` (`testid`);
ALTER TABLE `test`
ADD CONSTRAINT `test_fk1` FOREIGN KEY (`subid`) REFERENCES `subject` (`subid`),
ADD CONSTRAINT `test_fk2` FOREIGN KEY (`tcid`) REFERENCES `testconductor` (`tcid`);
ALTER TABLE `subject`
ADD CONSTRAINT `subject_fk1` FOREIGN KEY (`tcid`) REFERENCES `testconductor` (`tcid`);
INSERT INTO `adminlogin` VALUES ('001','root','root');
INSERT INTO `studenttest` (`stdid`, `testid`, `starttime`, `endtime`, `correctlyanswered`,
`status`) VALUES
(1, 1, '2014-10-15 09:11:24', '2014-10-15 09:21:24', 0, 'over');
INSERT INTO `subject` (`subid`, `subname`, `subdesc`, `tcid`) VALUES
(1, 'fref', 'few', NULL);
INSERT INTO `test` (`testid`, `testname`, `testdesc`, `testdate`, `testtime`, `subid`,
`testfrom`, `testto`, `duration`, `totalquestions`, `attemptedstudents`, `testcode`, `tcid`)
VALUES
(1, 'gregre', 'greger', '2014-10-15', '17:08:16', 1, '2014-10-15 03:08:16', '2014-10-16
15:59:59', 10, 2, 0, '.ȁ', NULL);
When the student has complete the test, then the question will not be able to delete. Neither the test or subject.
It is because the studentquestion table will be left with a foreign key that points to a non existent record. You'll either need to remove rows which have a foreign key to the record you want to delete or, alternatively set up CASCADE rules so that when parent rows are deleted action can be taken.

MySQL returns only one result

This is my product table:
CREATE TABLE `products` (
`idproduct` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`description` text NOT NULL,
`price` double unsigned NOT NULL,
`shipping` double unsigned DEFAULT NULL,
`condition` int(10) unsigned NOT NULL,
`returns` int(10) unsigned NOT NULL,
`date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`image` varchar(255) DEFAULT NULL,
PRIMARY KEY (`idproduct`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8
And this is my product_categories table
CREATE TABLE `products_subcategories` (
`idproduct_category` int(10) unsigned NOT NULL AUTO_INCREMENT,
`idproduct` int(10) unsigned NOT NULL,
`idsubcategory` int(10) unsigned NOT NULL,
PRIMARY KEY (`idproduct_category`),
KEY `products_categories_products_idx` (`idproduct`),
KEY `products_categories_categories_idx` (`idsubcategory`),
CONSTRAINT `products_subcategories_products` FOREIGN KEY (`idproduct`) REFERENCES `products` (`idproduct`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `products_subcategories_subcategories` FOREIGN KEY (`idsubcategory`) REFERENCES `subcategories` (`idSubcategory`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
I trying to search in products like that:
Select products.* from products
INNER JOIN products_subcategories
ON products.idproduct = products_subcategories.idproduct
WHERE name LIKE '%p%'
but it returns only one row.. without inner join it returns me more rows