I'm really frusterated with these codes and I can't find where the errors are..
SSH secure shell .txt file with the error codes:
Table created.
SQL>
SQL> CREATE TABLE Order_mys (
2 OrderID NUMBER(3) NOT NULL,
3 OrderDate DATE NOT NULL,
4 CustID CHAR(5) NOT NULL,
5 PRIMARY KEY (OrderID),
6 FOREIGN KEY (CustID) REFERENCES Customer_mys
7 ) ;
CREATE TABLE Order_mys (
*
ERROR at line 1:
ORA-00955: name is already used by an existing object
SQL>
SQL> CREATE TABLE Product_mys (
2 ProductID NUMBER(3) NOT NULL,
3 ProductName VARCHAR(30) NOT NULL,
4 CatID Number(3) NOT NULL,
5 PRIMARY KEY (ProductID),
6 FOREIGN KEY (CatID) REFERENCES Category_mys
7 ) ;
CREATE TABLE Product_mys (
*
ERROR at line 1:
ORA-00955: name is already used by an existing object
SQL>
SQL> CREATE TABLE OrderDetail_mys (
2 OrderID NUMBER(3) NOT NULL,
3 ProductID NUMBER(3) NOT NULL,
4 ProductQty NUMBER(4) NOT NULL,
5 ProdcutPrice NUMBER(6,2) NOT NULL,
6 PRIMARY KEY (OrderID, ProductID),
7 FOREIGN KEY (OrderID) REFERENCES Order_mys,
8 FOREIGN KEY (ProductID) REFERENCES Product_mys
9 ) ;
CREATE TABLE OrderDetail_mys (
*
ERROR at line 1:
ORA-00955: name is already used by an existing object
SQL>
SQL>
SQL> DESCRIBE Dept_mys ;
Name Null? Type
----------------------------------------- -------- ----------------------------
DEPTID NOT NULL NUMBER(3)
DEPTNAME NOT NULL VARCHAR2(20)
SQL> DESCRIBE Commission_mys ;
Name Null? Type
----------------------------------------- -------- ----------------------------
COMMCLASS NOT NULL CHAR(1)
COMMRATE NOT NULL NUMBER(2,2)
SQL> DESCRIBE Category_mys ;
Name Null? Type
----------------------------------------- -------- ----------------------------
CATID NOT NULL NUMBER(3)
CATNAME NOT NULL VARCHAR2(20)
SQL> DESCRIBE SalesRep_mys ;
Name Null? Type
----------------------------------------- -------- ----------------------------
SALESREPID NOT NULL NUMBER(4)
SALESREPFNAME NOT NULL VARCHAR2(20)
SALESREPLNAME NOT NULL VARCHAR2(20)
DEPTID NOT NULL NUMBER(3)
COMMCLASS NOT NULL CHAR(1)
SQL> DESCRIBE Customer_mys ;
Name Null? Type
----------------------------------------- -------- ----------------------------
CUSTID NOT NULL VARCHAR2(5)
CUSTFNAME NOT NULL VARCHAR2(20)
CUSTLNAME NOT NULL VARCHAR2(20)
CUSTPHONE CHAR(10)
SALESREPID NOT NULL NUMBER(4)
SQL> DESCRIBE Order_mys ;
Name Null? Type
----------------------------------------- -------- ----------------------------
ORDERID NOT NULL NUMBER(3)
ORDERDATE NOT NULL DATE
CUSTID NOT NULL CHAR(5)
SQL> DESCRIBE Product_mys ;
Name Null? Type
----------------------------------------- -------- ----------------------------
PRODUCTID NOT NULL NUMBER(3)
PRODUCTNAME NOT NULL VARCHAR2(30)
CATID NOT NULL NUMBER(3)
SQL> DESCRIBE OrderDetail_mys ;
Name Null? Type
----------------------------------------- -------- ----------------------------
ORDERID NOT NULL NUMBER(3)
PRODUCTID NOT NULL NUMBER(3)
PRODUCTQTY NOT NULL NUMBER(4)
PRODCUTPRICE NOT NULL NUMBER(6,2)
SQL>
SQL> --Part II
SQL>
SQL> INSERT INTO Dept_mys
2 VALUES (10, 'Store Sales') ;
1 row created.
SQL>
SQL> INSERT INTO Dept_mys
2 VALUES (14, 'Corp Sales') ;
1 row created.
SQL>
SQL> INSERT INTO Dept_mys
2 VALUES (16, 'Web Sales') ;
1 row created.
SQL>
SQL>
SQL>
SQL> INSERT INTO Commission_mys
2 VALUES (A, .1) ;
VALUES (A, .1)
*
ERROR at line 2:
ORA-00984: column not allowed here
SQL>
SQL> INSERT INTO Commission_mys
2 VALUES (B, .08) ;
VALUES (B, .08)
*
ERROR at line 2:
ORA-00984: column not allowed here
SQL>
SQL> INSERT INTO Commission_mys
2 VALUES (Z, 0) ;
VALUES (Z, 0)
*
ERROR at line 2:
ORA-00984: column not allowed here
SQL>
SQL> INSERT INTO Commission_mys
2 VALUES (C, .05) ;
VALUES (C, .05)
*
ERROR at line 2:
ORA-00984: column not allowed here
SQL>
SQL>
SQL> INSERT INTO Category_mys
2 VALUES (1, 'Hand Tools') ;
1 row created.
SQL>
SQL> INSERT INTO Category_mys
2 VALUES (2, 'power Tools') ;
1 row created.
SQL>
SQL> INSERT INTO Category_mys
2 VALUES (4, 'Fasteners') ;
1 row created.
SQL>
SQL> INSERT INTO Category_mys
2 VALUES (6, 'Misc') ;
1 row created.
SQL>
SQL> INSERT INTO Category_mys
2 VALUES (3, 'Measuring Tools') ;
1 row created.
SQL>
SQL> INSERT INTO Category_mys
2 VALUES (5, 'Hardware') ;
1 row created.
SQL>
SQL>
SQL>
SQL> INSERT INTO SalesRep_mys
2 VALUES (10, 'Alice', 'Jones', 10, 'A') ;
INSERT INTO SalesRep_mys
*
ERROR at line 1:
ORA-02291: integrity constraint (MYS2306.SYS_C00966867) violated - parent key
not found
SQL>
SQL> INSERT INTO SalesRep_mys
2 VALUES (12, 'Greg', 'Taylor', 14, 'B') ;
INSERT INTO SalesRep_mys
*
ERROR at line 1:
ORA-02291: integrity constraint (MYS2306.SYS_C00966867) violated - parent key
not found
SQL>
SQL> INSERT INTO SalesRep_mys
2 VALUES (14, 'Sara', 'Day', 10, 'Z') ;
INSERT INTO SalesRep_mys
*
ERROR at line 1:
ORA-02291: integrity constraint (MYS2306.SYS_C00966867) violated - parent key
not found
SQL>
SQL> INSERT INTO SalesRep_mys
2 VALUES (8, 'Kay', 'Price', 14, 'C') ;
INSERT INTO SalesRep_mys
*
ERROR at line 1:
ORA-02291: integrity constraint (MYS2306.SYS_C00966867) violated - parent key
not found
SQL>
SQL> INSERT INTO SalesRep_mys
2 VALUES (20, 'Bob', 'Jackson', 10, 'B') ;
INSERT INTO SalesRep_mys
*
ERROR at line 1:
ORA-02291: integrity constraint (MYS2306.SYS_C00966867) violated - parent key
not found
SQL>
SQL> INSERT INTO SalesRep_mys
2 VALUES (22, 'Micah', 'Moore', 16, 'Z') ;
INSERT INTO SalesRep_mys
*
ERROR at line 1:
ORA-02291: integrity constraint (MYS2306.SYS_C00966867) violated - parent key
not found
SQL>
SQL>
SQL>
SQL> INSERT INTO Customer_mys
2 VALUES (S100, 'John', 'Smith', '5551212', 10) ;
VALUES (S100, 'John', 'Smith', '5551212', 10)
*
ERROR at line 2:
ORA-00984: column not allowed here
SQL>
SQL> INSERT INTO Customer_mys
2 VALUES (A120, 'Jane', 'Adams', '817555', 14) ;
VALUES (A120, 'Jane', 'Adams', '817555', 14)
*
ERROR at line 2:
ORA-00984: column not allowed here
SQL>
SQL> INSERT INTO Customer_mys
2 VALUES (J090, 'Tim', 'Jones', NULL, 10) ;
VALUES (J090, 'Tim', 'Jones', NULL, 10)
*
ERROR at line 2:
ORA-00984: column not allowed here
SQL>
SQL> INSERT INTO Customer_mys
2 VALUES (B200, 'Ann', 'Brown', '972555', 14) ;
VALUES (B200, 'Ann', 'Brown', '972555', 14)
*
ERROR at line 2:
ORA-00984: column not allowed here
Here's the tables that I did with the relationships, foreign key, inserts
DROP TABLE Customer_mys ;
DROP TABLE SalesRep_mys ;
DROP TABLE Category_mys ;
DROP TABLE Commission_mys ;
DROP TABLE Dept_mys ;
--part I
CREATE TABLE Dept_mys (
DeptID Number(3) NOT NULL,
DeptName VARCHAR(20) NOT NULL,
PRIMARY KEY (DeptID)
) ;
CREATE TABLE Commission_mys (
CommClass CHAR(1) NOT NULL,
CommRate Number(2,2) NOT NULL,
PRIMARY KEY (CommClass)
) ;
CREATE TABLE Category_mys (
CatID Number(3) NOT NULL,
catName VARCHAR(20) NOT NULL,
PRIMARY KEY (CatID)
) ;
CREATE TABLE SalesRep_mys (
SalesRepID NUMBER(4) NOT NULL,
SalesRepFName VARCHAR(20) NOT NULL,
SalesRepLName VARCHAR(20) NOT NULL,
DeptID NUMBER(3) NOT NULL,
CommClass CHAR(1) NOT NULL,
PRIMARY KEY (SalesRepID),
FOREIGN KEY (DeptID) REFERENCES Dept_mys,
FOREIGN KEY (CommClass) REFERENCES Commission_mys
) ;
CREATE TABLE Customer_mys (
CustID VARCHAR(5) NOT NULL,
CustFName VARCHAR(20) NOT NULL,
CustLName VARCHAR(20) NOT NULL,
CustPhone CHAR(10),
SalesRepID NUMBER(4) NOT NULL,
PRIMARY KEY (CustID),
FOREIGN KEY (SalesRepID) REFERENCES Customer_mys
) ;
CREATE TABLE Order_mys (
OrderID NUMBER(3) NOT NULL,
OrderDate DATE NOT NULL,
CustID CHAR(5) NOT NULL,
PRIMARY KEY (OrderID),
FOREIGN KEY (CustID) REFERENCES Customer_mys
) ;
CREATE TABLE Product_mys (
ProductID NUMBER(3) NOT NULL,
ProductName VARCHAR(30) NOT NULL,
CatID Number(3) NOT NULL,
PRIMARY KEY (ProductID),
FOREIGN KEY (CatID) REFERENCES Category_mys
) ;
CREATE TABLE OrderDetail_mys (
OrderID NUMBER(3) NOT NULL,
ProductID NUMBER(3) NOT NULL,
ProductQty NUMBER(4) NOT NULL,
ProdcutPrice NUMBER(6,2) NOT NULL,
PRIMARY KEY (OrderID, ProductID),
FOREIGN KEY (OrderID) REFERENCES Order_mys,
FOREIGN KEY (ProductID) REFERENCES Product_mys
) ;
DESCRIBE Dept_mys ;
DESCRIBE Commission_mys ;
DESCRIBE Category_mys ;
DESCRIBE SalesRep_mys ;
DESCRIBE Customer_mys ;
DESCRIBE Order_mys ;
DESCRIBE Product_mys ;
DESCRIBE OrderDetail_mys ;
--Part II
INSERT INTO Dept_mys
VALUES (10, 'Store Sales') ;
INSERT INTO Dept_mys
VALUES (14, 'Corp Sales') ;
INSERT INTO Dept_mys
VALUES (16, 'Web Sales') ;
INSERT INTO Commission_mys
VALUES (A, .1) ;
INSERT INTO Commission_mys
VALUES (B, .08) ;
INSERT INTO Commission_mys
VALUES (Z, 0) ;
INSERT INTO Commission_mys
VALUES (C, .05) ;
INSERT INTO Category_mys
VALUES (1, 'Hand Tools') ;
INSERT INTO Category_mys
VALUES (2, 'power Tools') ;
INSERT INTO Category_mys
VALUES (4, 'Fasteners') ;
INSERT INTO Category_mys
VALUES (6, 'Misc') ;
INSERT INTO Category_mys
VALUES (3, 'Measuring Tools') ;
INSERT INTO Category_mys
VALUES (5, 'Hardware') ;
INSERT INTO SalesRep_mys
VALUES (10, 'Alice', 'Jones', 10, A) ;
INSERT INTO SalesRep_mys
VALUES (12, 'Greg', 'Taylor', 14, B) ;
INSERT INTO SalesRep_mys
VALUES (14, 'Sara', 'Day', 10, Z) ;
INSERT INTO SalesRep_mys
VALUES (8, 'Kay', 'Price', 14, C) ;
INSERT INTO SalesRep_mys
VALUES (20, 'Bob', 'Jackson', 10, B) ;
INSERT INTO SalesRep_mys
VALUES (22, 'Micah', 'Moore', 16, Z) ;
INSERT INTO Customer_mys
VALUES (S100, 'John', 'Smith', '5551212', 10) ;
INSERT INTO Customer_mys
VALUES (A120, 'Jane', 'Adams', '817555', 14) ;
INSERT INTO Customer_mys
VALUES (J090, 'Tim', 'Jones', NULL, 10) ;
INSERT INTO Customer_mys
VALUES (B200, 'Ann', 'Brown', '972555', 14) ;
INSERT INTO Customer_mys
VALUES (G070, 'Kate', 'Green', NULL, 10) ;
INSERT INTO Customer_mys
VALUES (S120, 'Nicole', 'Sims', NULL, 16) ;
INSERT INTO Order_mys
VALUES (100, '24-Jan-15', S100) ;
INSERT INTO Order_mys
VALUES (101, '25-Jan-15', A120) ;
INSERT INTO Order_mys
VALUES (102, '26-Jan-15', J090) ;
INSERT INTO Order_mys
VALUES (105, '26-Jan-15', B200) ;
INSERT INTO Order_mys
VALUES (106, '27-Jan-15', G070) ;
INSERT INTO Order_mys
VALUES (108, '27-Jan-15', S120) ;
INSERT INTO Product_mys
VALUES (121, 'BD Hammer', 1) ;
INSERT INTO Product_mys
VALUES (228, 'Makita Power Drill', 2) ;
INSERT INTO Product_mys
VALUES (480, '1 # BD Nails', 4) ;
INSERT INTO Product_mys
VALUES (610, '3M Duct Tape', 6) ;
INSERT INTO Product_mys
VALUES (618, '3M Masking Tape', 6) ;
INSERT INTO Product_mys
VALUES (380, 'Acme Yard Stick', 3) ;
INSERT INTO Product_mys
VALUES (535, 'Schlage Door Knob', 5) ;
INSERT INTO Product_mys
VALUES (123, 'Acme Pry Bar', 1) ;
INSERT INTO Product_mys
VALUES (229, 'BD Power Drill', 2) ;
INSERT INTO Product_mys
VALUES (124, 'Acme Hammer', 1) ;
INSERT INTO Product_mys
VALUES (235, 'Makita Power Drill', 2) ;
INSERT INTO OrderDetail_mys
VALUES (100, 121, 2, 8.00) ;
INSERT INTO OrderDetail_mys
VALUES (100, 228, 2, 65.00) ;
INSERT INTO OrderDetail_mys
VALUES (100, 480, 2, 3.00) ;
INSERT INTO OrderDetail_mys
VALUES (100, 407, 1, 4.25) ;
INSERT INTO OrderDetail_mys
VALUES (101, 610, 200, 1.75) ;
INSERT INTO OrderDetail_mys
VALUES (101, 618, 100, 1.25) ;
INSERT INTO OrderDetail_mys
VALUES (101, 121, 2, 8.00) ;
INSERT INTO OrderDetail_mys
VALUES (102, 380, 2, 1.25) ;
INSERT INTO OrderDetail_mys
VALUES (102, 121, 1, 7.00) ;
INSERT INTO OrderDetail_mys
VALUES (102, 535, 4, 7.50) ;
INSERT INTO OrderDetail_mys
VALUES (103, 121, 50, 7.00) ;
INSERT INTO OrderDetail_mys
VALUES (103, 123, 20, 6.25) ;
INSERT INTO OrderDetail_mys
VALUES (101, 121, 2, 8.00) ;
INSERT INTO OrderDetail_mys
VALUES (104, 229, 1, 50.00) ;
INSERT INTO OrderDetail_mys
VALUES (104, 610, 200, 1.75) ;
INSERT INTO OrderDetail_mys
VALUES (104, 380, 2, 1.25) ;
INSERT INTO OrderDetail_mys
VALUES (104, 535, 4, 7.50) ;
INSERT INTO OrderDetail_mys
VALUES (101, 121, 2, 8.00) ;
INSERT INTO OrderDetail_mys
VALUES (105, 610, 200, 1.75) ;
INSERT INTO OrderDetail_mys
VALUES (105, 123, 40, 5.00) ;
INSERT INTO OrderDetail_mys
VALUES (106, 124, 1, 6.50) ;
INSERT INTO OrderDetail_mys
VALUES (107, 229, 1, 59.00) ;
INSERT INTO OrderDetail_mys
VALUES (108, 235, 1, 65.00) ;
COMMIT ;
SELECT * FROM Dept_mys ;
SELECT * FROM Commission_mys ;
SELECT * FROM SalesRep_mys ;
SELECT * FROM Customer_mys ;
SELECT * FROM Category_mys ;
SELECT * FROM Order_mys ;
SELECT * FROM Product_mys ;
SELECT * FROM OrderDetail_mys ;
--PART III
UPDATE Customer_mys
SET CustLName = 'Thompson'
WHERE CustID = 'A120' ;
UPDATE Customer_mys
SET CustPhone = '2146881234'
WHERE CustID = 'J090' ;
UPDATE Product_mys
SET ProductPrice = 7.50
WHERE ProductID = '121' ;
UPDATE OrderDetail_mys
SET ProductName = 'BD Claw Hammer' ;
INSERT INTO Product_mys
VALUES (122, 'BD Rubber Mallet', 1) ;
INSERT INTO Product_mys
VALUES (125, 'Stanley Chisel', 1) ;
INSERT INTO Product_mys
VALUES (126, 'BD Chisel', 1) ;
INSERT INTO Product_mys
VALUES (381, 'BD Measuring Tape', 3) ;
INSERT INTO Product_mys
VALUES (382, 'Stanley Meausring Tape', 3) ;
INSERT INTO Commission_mys
VALUES (D, .03) ;
INSERT INTO SalesRep_mys
VALUES (21, 'Mark', 'Anderson', 14, C) ;
INSERT INTO SalesRep_mys
VALUES (23, 'Jennie', 'Hayes', 10, D) ;
INSERT INTO OrderDetail_mys
VALUES (109, 228, 1, 62.50) ;
INSERT INTO OrderDetail_mys
VALUES (109, 407, 1, 4.00) ;
INSERT INTO OrderDetail_mys
VALUES (109, 381, 2, 7.75) ;
INSERT INTO OrderDetail_mys
VALUES (110, 122, 2, 7.00) ;
INSERT INTO OrderDetail_mys
VALUES (110, 124, 1, 6.75) ;
INSERT INTO OrderDetail_mys
VALUES (110, 480, 2, 3.25) ;
COMMIT ;
SELECT * FROM Dept_mys ;
SELECT * FROM Commission_mys ;
SELECT * FROM SalesRep_mys ;
SELECT * FROM Customer_mys ;
SELECT * FROM Category_mys ;
SELECT * FROM Order_mys ;
SELECT * FROM Product_mys ;
SELECT * FROM OrderDetail_mys ;
I believe you're getting this error because A and B are being interpreted as some kind of variable. Try modifying your query to
INSERT INTO SalesRep_mys
VALUES (10, 'Alice', 'Jones', 10, 'A') ;
INSERT INTO SalesRep_mys
VALUES (12, 'Greg', 'Taylor', 14, 'B') ;
Related
Require SQL and/or JPQL statement to retrieve latest record for a given account sorted by date (descending). It should return a single record (latest record) for all users e.g. if 'user a' sends/receives multiple message from 'user b', 'user c', and 'user d', it would return the latest record for each user (3 in total).
My Attempt at JPQL:
SELECT DISTINCT dm FROM DirectMessage dm WHERE (dm.messageFrom.id=1 OR dm.messageTo.id=1) GROUP BY dm.id, dm.messageFrom.id, dm.messageTo.id ORDER BY dm.date DESC
Concrete Example with schema:
if I wished to return the latest record for 'user a' then correct number of record returned would be 3 (insert statement for each of these 3 records below):
1.INSERT INTO direct_message VALUE(2, 2, 1, 'hello back', now());
2.INSERT INTO direct_message VALUE(4, 3, 1, 'hello back', now());
3.INSERT INTO direct_message VALUE(6, 4, 1, 'hello back', now());
Script
CREATE TABLE account(
id bigint UNSIGNED NOT NULL auto_increment,
username varchar(75) NOT NULL,
name varchar(70) NOT NULL,
date datetime NOT NULL,
CONSTRAINT pkey_account_id PRIMARY KEY(id)
);
INSERT INTO account VALUES (1,'user_a','User A', now());
INSERT INTO account VALUES (2,'user_b','User B', now());
INSERT INTO account VALUES (3,'user_c','User C', now());
INSERT INTO account VALUES (4,'user_d','User D', now());
CREATE TABLE direct_message(
id bigint UNSIGNED NOT NULL auto_increment,
message_from_id bigint UNSIGNED NOT NULL,
message_to_id bigint UNSIGNED NOT NULL,
message_text varchar(200) NOT NULL,
date datetime NOT NULL,
CONSTRAINT pkey_dm_id PRIMARY KEY(id),
CONSTRAINT fk_direct_message_to FOREIGN KEY (message_to_id) REFERENCES account(id),
CONSTRAINT fk_message_from FOREIGN KEY (message_from_id) REFERENCES account(id)
);
INSERT INTO direct_message VALUE(1, 1, 2, 'hello', now());
INSERT INTO direct_message VALUE(2, 2, 1, 'hello back', now());
INSERT INTO direct_message VALUE(3, 1, 3, 'hello', now());
INSERT INTO direct_message VALUE(4, 3, 1, 'hello back', now());
INSERT INTO direct_message VALUE(5, 1, 4, 'hello', now());
INSERT INTO direct_message VALUE(6, 4, 1, 'hello back', now());
INSERT INTO direct_message VALUE(7, 2, 4, 'hello', now());
INSERT INTO direct_message VALUE(8, 4, 2, 'hello back', now());
I think you want:
select dm.*
from direct_message dm
where #userid in (dm.message_from_id, dm.message_to_id) and
dm.date = (select max(dm2.date)
from direct_message dm2
where (dm2.message_from_id, dm2.message_to_id) in ( (dm.message_from_id, dm.message_to_id), (dm.message_to_id, dm.message_from_id) )
);
EDIT:
It seems strange to have different messages with exactly the same date/time stamp, but you can distinguish them using the id:
select dm.*
from direct_message dm
where 1 in (dm.message_from_id, dm.message_to_id) and
dm.id = (select dm2.id
from direct_message dm2
where (dm2.message_from_id, dm2.message_to_id) in ( (dm.message_from_id, dm.message_to_id), (dm.message_to_id, dm.message_from_id) )
order by dm2.date desc
limit 1
);
so im sure this won't tax anyone but new to all this! looking for a way to pull out data from an audit table which gives a view of all the created rows where they have not subsequently been deleted.
so by example...
CREATE TABLE `test1` (
`tID` INT(11) NULL DEFAULT NULL,
`tDate` DATE NULL DEFAULT NULL,
`tName` VARCHAR(50) NULL DEFAULT NULL,
`tType` VARCHAR(50) NULL DEFAULT NULL,
`tAction` VARCHAR(50) NULL DEFAULT NULL
)
INSERT INTO `test1` VALUES (1, '2019-02-01', 'Bob', 'a', 'Create');
INSERT INTO `test1` VALUES (2, '2019-02-02', 'Frank', 'a', 'Create');
INSERT INTO `test1` VALUES (3, '2019-02-03', 'Jim', 'b', 'Create');
INSERT INTO `test1` VALUES (4, '2019-02-04', 'Frank', 'a', 'Delete');
INSERT INTO `test1` VALUES (5, '2019-02-05', 'Bob', 'b', 'Create');
INSERT INTO `test1` VALUES (6, '2019-02-06', 'Bob', 'a', 'Delete');
INSERT INTO `test1` VALUES (7, '2019-02-07', 'Bob', 'a', 'Create');
INSERT INTO `test1` VALUES (8, '2019-02-08', 'Frank', 'b', 'Create');
INSERT INTO `test1` VALUES (9, '2019-02-09', 'Bob', 'b', 'Delete');
INSERT INTO `test1` VALUES (10, '2019-02-10', 'Bob', 'b', 'Create');
INSERT INTO `test1` VALUES (11, '2019-02-11', 'kate', 'a', 'Create');
INSERT INTO `test1` VALUES (12, '2019-02-12', 'kate', 'a', 'Delete');
Want the output to be something like...
"Bob" "2019-02-07" "a"
"Jim" "2019-02-03" "b"
"Bob" "2019-02-10" "b"
"Frank" "2019-02-08" "b"
so i can can get max where create easy enough but how should i exclude the ones that have been deleted after creation?
this gets me close... but i need to remove the ones with a later deleted row..
SELECT
max(tDate),
tname,
tType
FROM test1
WHERE tAction = 'create'
GROUP BY tname, ttype
thanks for any help! j.
select *
from test1 t1
where t1.tAction <> 'Delete'
and not exists
(
select *
from test1 t2
where t2.tName = t1.tName -- same user
and t2.tType = t1.tType -- and same type
and t2.tAction = 'Delete' -- deleted
and t1.tDate < t2.tDate -- at a later time
)
Try using correlated subquery
DEMO
select * from test1 a where a.taction='Create' and not exists
(
select 1 from test1 b where a.tname=b.tname and a.ttype=b.ttype
and a.tdate<b.tdate and b.taction<>'Create'
)
OUTPUT:
tID tDate tName tType tAction
3 2019-02-03 Jim b Create
7 2019-02-07 Bob a Create
8 2019-02-08 Frank b Create
10 2019-02-10 Bob b Create
SELECT
tDate,
tname,
tType
FROM test1
WHERE tAction <> 'Delete'
I hope this is the query you are looking for.
Reference
SQL: How to perform string does not equal
I am still a beginner in mySQL. I have been trying to figure out a trigger for my project but can't come up with a solution. Here is what I am trying to do:
When an order is made I want a trigger to update the inventory. The bump here is that orders can have many products and those products are composed of different elements of the inventory. Here is how I tried it maybe it can help you understand the idea.
create trigger subtract_inv
after insert on order_details for each row
begin
update inventory
set Inv_qty = Inv_qty - (order_details.Order_details_qty * recipes.Requiered_qty)
where Products_Prod_ID = NEW.Products_Prod_ID and inventory.Inv_ID = recipes.Inventory_Inv_ID;
end$$
delimiter ;
Here are the 3 tables
-- -----------------------------------------------------
-- Table `McDo`.`Order_details`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `McDo`.`Order_details` (
`orders_Order_id` INT NOT NULL,
`Products_Prod_ID` INT NOT NULL,
`Order_details_qty` INT NULL,
PRIMARY KEY (`orders_Order_id`, `Products_Prod_ID`),
INDEX `fk_orders_has_Products_Products1_idx` (`Products_Prod_ID`
ASC) VISIBLE,
INDEX `fk_Order_details_orders1_idx` (`orders_Order_id` ASC)
VISIBLE,
CONSTRAINT `fk_orders_has_Products_Products1`
FOREIGN KEY (`Products_Prod_ID`)
REFERENCES `McDo`.`Products` (`Prod_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Order_details_orders1`
FOREIGN KEY (`orders_Order_id`)
REFERENCES `McDo`.`Orders` (`Order_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `McDo`.`Inventory`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `McDo`.`Inventory` (
`Inv_ID` INT NOT NULL,
`Inv_type` VARCHAR(1) NOT NULL,
`Inv_name` VARCHAR(45) NULL,
`Inv_qty` INT NULL,
`Inv_price_unit` DECIMAL(20,2) NULL,
PRIMARY KEY (`Inv_ID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `McDo`.`Recipes`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `McDo`.`Recipes` (
`Products_Prod_ID` INT NOT NULL,
`Inventory_Inv_ID` INT NOT NULL,
`Requiered_qty` INT NULL,
PRIMARY KEY (`Products_Prod_ID`, `Inventory_Inv_ID`),
INDEX `fk_Products_has_Inventory_Products1_idx` (`Products_Prod_ID` ASC)
VISIBLE,
INDEX `fk_Products_has_Inventory_Inventory1_idx` (`Inventory_Inv_ID`
ASC) VISIBLE,
CONSTRAINT `fk_Products_has_Inventory_Products1`
FOREIGN KEY (`Products_Prod_ID`)
REFERENCES `McDo`.`Products` (`Prod_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Products_has_Inventory_Inventory1`
FOREIGN KEY (`Inventory_Inv_ID`)
REFERENCES `McDo`.`Inventory` (`Inv_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
and here a sample of the data
-- -----------------------------------------------------
-- Data for table `McDo`.`Recipes`
-- -----------------------------------------------------
START TRANSACTION;
USE `McDo`;
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (301, 102, 3);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (301, 103, 2);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (301, 112, 1);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (301, 110, 1);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (301, 111, 2);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (301, 114, 1);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (301, 113, 1);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (301, 108, 1);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (302, 101, 2);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (302, 111, 2);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (302, 103, 2);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (302, 107, 1);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (302, 113, 1);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (302, 114, 1);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (302, 108, 1);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (302, 109, 1);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (301, 204, 1);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (302, 202, 1);
-- -----------------------------------------------------
-- Data for table `McDo`.`Inventory`
-- -----------------------------------------------------
START TRANSACTION;
USE `McDo`;
INSERT INTO `McDo`.`Inventory` (`Inv_ID`, `Inv_type`, `Inv_name`, `Inv_qty`, `Inv_price_unit`) VALUES (101, 'C', 'regular bun', 1000, 0.07);
INSERT INTO `McDo`.`Inventory` (`Inv_ID`, `Inv_type`, `Inv_name`, `Inv_qty`, `Inv_price_unit`) VALUES (102, 'C', 'sesame bun', 1000, 0.12);
INSERT INTO `McDo`.`Inventory` (`Inv_ID`, `Inv_type`, `Inv_name`, `Inv_qty`, `Inv_price_unit`) VALUES (103, 'C', 'beef patty', 1000, 0.35);
INSERT INTO `McDo`.`Inventory` (`Inv_ID`, `Inv_type`, `Inv_name`, `Inv_qty`, `Inv_price_unit`) VALUES (107, 'C', 'ketchup sachet', 5000, 0.02);
INSERT INTO `McDo`.`Inventory` (`Inv_ID`, `Inv_type`, `Inv_name`, `Inv_qty`, `Inv_price_unit`) VALUES (108, 'C', 'seasoning sachet', 5000, 0.01);
INSERT INTO `McDo`.`Inventory` (`Inv_ID`, `Inv_type`, `Inv_name`, `Inv_qty`, `Inv_price_unit`) VALUES (109, 'C', 'mustard sachet', 5000, 0.01);
INSERT INTO `McDo`.`Inventory` (`Inv_ID`, `Inv_type`, `Inv_name`, `Inv_qty`, `Inv_price_unit`) VALUES (110, 'C', 'big mac sauce sachet', 5000, 0.04);
INSERT INTO `McDo`.`Inventory` (`Inv_ID`, `Inv_type`, `Inv_name`, `Inv_qty`, `Inv_price_unit`) VALUES (111, 'C', 'cheese slice', 2000, 0.05);
INSERT INTO `McDo`.`Inventory` (`Inv_ID`, `Inv_type`, `Inv_name`, `Inv_qty`, `Inv_price_unit`) VALUES (112, 'C', 'precut letuce', 1500, 0.02);
INSERT INTO `McDo`.`Inventory` (`Inv_ID`, `Inv_type`, `Inv_name`, `Inv_qty`, `Inv_price_unit`) VALUES (113, 'C', 'precut onion', 1500, 0.01);
INSERT INTO `McDo`.`Inventory` (`Inv_ID`, `Inv_type`, `Inv_name`, `Inv_qty`, `Inv_price_unit`) VALUES (114, 'C', 'precut pickle', 1500, 0.01);
INSERT INTO `McDo`.`Inventory` (`Inv_ID`, `Inv_type`, `Inv_name`, `Inv_qty`, `Inv_price_unit`) VALUES (202, 'N', 'mc double wrapping', 1000, 0.01);
INSERT INTO `McDo`.`Inventory` (`Inv_ID`, `Inv_type`, `Inv_name`, `Inv_qty`, `Inv_price_unit`) VALUES (204, 'N', 'big mac box', 1000, 0.02);
and orders can look like:
1000,301,2;
1000,302,1;
1001,301,2;
1002,302,3;
You are allowed to use joins in an update query so perhaps this
delimiter $$
CREATE TRIGGER `subtract_inv` AFTER INSERT ON `order_details` FOR EACH ROW
begin
update inventory join
(select r.inventory_inv_id rinvid,new.order_details_qty * r.Requiered_qty adjustment
from recipes r join inventory i on i.inv_id = r.inventory_inv_id
where r.products_prod_id = new.products_prod_id) s
set inv_qty = inv_qty - s.adjustment
where inventory.Inv_ID = s.rinvid;
end $$
delimiter ;
I have this MySQL DB :
MySQL in order to create DB + set of test value :
DROP DATABASE IF EXISTS test;
CREATE DATABASE test;
CREATE TABLE table_status ( Id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, Status CHAR(27) NOT NULL) ENGINE=INNODB;
INSERT INTO table_status (Id, Status) VALUES (1, 'CREATED'), (2, 'UNSURE'), (3, 'RUNNING'), (4, 'BEGIN_ACTION_TRUE'), (5, 'STOPPED_ACTION_TRUE_OK'), (6, 'STOPPED_ACTION_TRUE_NOT_OK'), (7, 'BEGIN_ACTION_FALSE'), (8, 'STOPPED_ACTION_FALSE_OK'), (9, 'STOPPED_ACTION_FALSE_NOT_OK'), (10, 'STOPPED_NOT_OK'), (11, 'STOPPED_OK'), (12, 'CANCEL_REQUESTED'), (13, 'CANCELLED');
CREATE TABLE table_group ( Id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, Status TINYINT UNSIGNED NOT NULL DEFAULT 1, CONSTRAINT fk_group_Status FOREIGN KEY (Status) REFERENCES table_status(Id)) ENGINE=INNODB;
CREATE TABLE table_task ( Id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, IdGroup BIGINT UNSIGNED NOT NULL, Status TINYINT UNSIGNED NOT NULL DEFAULT 1, Command TEXT NOT NULL, CONSTRAINT fk_task_IdGroup FOREIGN KEY (IdGroup) REFERENCES table_group(Id) ON DELETE CASCADE, CONSTRAINT fk_task_Status FOREIGN KEY (Status) REFERENCES table_status(Id)) ENGINE=INNODB;
CREATE TABLE table_dependency ( IdBeforeTask BIGINT UNSIGNED NOT NULL, IdAfterTask BIGINT UNSIGNED NOT NULL, PRIMARY KEY (IdBeforeTask, IdAfterTask), CONSTRAINT fk_dependency_IdBeforeTask FOREIGN KEY (IdBeforeTask) REFERENCES table_task(Id) ON DELETE CASCADE, CONSTRAINT fk_dependency_IdAfterTask FOREIGN KEY (IdAfterTask) REFERENCES table_task(Id) ON DELETE CASCADE) ENGINE=INNODB;
INSERT INTO table_group (Id) VALUES (1), (2), (3), (4);
INSERT INTO table_task (IdGroup, Command) VALUES (1, "command_group_1_task_1"), (1, "command_group_1_task_2"), (1, "command_group_1_task_3"), (2, "command_group_2_task_1"), (2, "command_group_2_task_2"), (2, "command_group_2_task_3"), (3, "command_group_3_task_1"), (3, "command_group_3_task_2"), (3, "command_group_3_task_3"), (4, "command_group_4_task_1"), (4, "command_group_4_task_2"), (4, "command_group_4_task_3");
INSERT INTO table_dependency (IdBeforeTask, IdAfterTask) VALUES (1, 2), (2, 3), (4, 5), (5, 6), (7, 8), (8, 9), (10, 11), (11, 12);
The data in table_status is fixed to this :
INSERT INTO table_status (Id, Status) VALUES
(1, 'CREATED')
(2, 'UNSURE')
(3, 'RUNNING')
(4, 'BEGIN_ACTION_TRUE')
(5, 'STOPPED_ACTION_TRUE_OK')
(6, 'STOPPED_ACTION_TRUE_NOT_OK')
(7, 'BEGIN_ACTION_FALSE')
(8, 'STOPPED_ACTION_FALSE_OK')
(9, 'STOPPED_ACTION_FALSE_NOT_OK')
(10, 'STOPPED_NOT_OK')
(11, 'STOPPED_OK')
(12, 'CANCEL_REQUESTED')
(13, 'CANCELLED')
The purpose is to have a group containing task, each task can be dependant on another one.
What I want to obtain :
Group.Id, NB_TASK_TOTAL, NB_TASK_CREATED, NB_TASK_UNSURE
But there is a trap : The number of NB_TASK_CREATED is defined as : the number of task with status to CREATED and with no unresolved dependency.
An unresolved dependency is defined as a task depend on another wich have a Status != 11 (STOPPED_OK).
So if I have this :
table_dependency :
IdBeforeTask IdAfterTask
1 2
2 3
table_task :
Id IdGroup Status Command
1 1 1 command_1
2 1 1 command_2
3 1 1 command_3
I want to have
Group.Id, NB_TASK_TOTAL, NB_TASK_CREATED, NB_TASK_UNSURE
1 3 1 0
Here my unsucesful try :
SELECT
G.Id,
(SELECT COUNT(T.Id) FROM table_task as T WHERE T.IdGroup = G.Id),
(SELECT COUNT(T.Status = 1) FROM table_task as T WHERE T.IdGroup = G.Id AND T.Id NOT IN (SELECT IdAfterTask from table_dependency as D1 join table_task as T1 on T1.Id=D1.IdBeforeTask WHERE T1.Status!=11)),
(SELECT COUNT(T.Status = 2) FROM table_task as T WHERE T.IdGroup = G.Id)
FROM
table_group as G
The problem is I obtain this :
Group.Id, NB_TASK_TOTAL, NB_TASK_CREATED, NB_TASK_UNSURE
1 3 1 3
2 3 0 3
(I have 4 group, each group containing 3 task, all the task with Status = 1 (CREATED) and the dependency set in order to have just on "resolved" dependency so i should have
Group.Id, NB_TASK_TOTAL, NB_TASK_CREATED, NB_TASK_UNSURE
1 3 1 0
2 3 1 0
instead.
I'm a beginner in MySQL, and i'm lost on this one request.
This should do it for you:
SELECT T.IdGroup as Group_Id,
COUNT(T.IdGroup) as NB_TASK_TOTAL,
(SELECT COUNT(Tt.Id) FROM table_task Tt WHERE Tt.IdGroup = T.IdGroup AND Tt.Status = 1) as NB_TASK_CREATED,
(SELECT COUNT(Tt.Id) FROM table_task Tt WHERE Tt.IdGroup = T.IdGroup AND Tt.Status = 2 AND Tt.Id NOT IN (SELECT IdAfterTask from table_dependency as D1 join table_task as T1 on T1.Id=D1.IdBeforeTask WHERE T1.Status!=11)) as NB_TASK_UNSURE
FROM table_task T
GROUP BY T.IdGroup;
I have a MySQL DB that needs to provide attributes associated with a taxonomy. I desire to have a list of all attributes, including "inherited attributes" for a given element in the taxonomy. I'm not an SQL expert & have learned (and plagiarized) much from this site. One thing I learned here is to use a Closure Table to represent my Hierarchy. What I need to do for my database is associate a large number of attributes to elements within hierarchy. However, I can't seem to figure out how to grab all of the attributes associated with a given node and all of its parents. I created the following example database for the purpose of this question (feel free to comment on anything about this schema, the fake data is just noise).
My example MySQL database structure looks like this:
-- Simple Sample
SET FOREIGN_KEY_CHECKS=0;
DROP TRIGGER IF EXISTS inheritance_tree_insert;
DROP TRIGGER IF EXISTS inheritance_tree_update;
DROP TABLE IF EXISTS inheritance_paths;
DROP TABLE IF EXISTS inheritance_tree;
DROP TABLE IF EXISTS attributes;
SET FOREIGN_KEY_CHECKS=1;
CREATE TABLE `inheritance_tree` (
`it_id` INT NOT NULL, -- PK
`parent` INT, -- Parent id & FK
`child_order` INT, -- Oder of siblings
`name` VARCHAR(500) NOT NULL, -- Name for the entry
PRIMARY KEY (`it_id`),
FOREIGN KEY (`parent`) REFERENCES inheritance_tree(`it_id`) ON DELETE CASCADE,
INDEX(`name`)
) ENGINE = INNODB;
-- Trigger to update the paths table for new entries
DELIMITER //
CREATE TRIGGER `inheritance_tree_insert` AFTER INSERT ON `inheritance_tree` FOR EACH ROW
BEGIN
INSERT INTO `inheritance_paths` (`ancestor`, `descendant`, `len`)
SELECT `ancestor`, NEW.`it_id`, len + 1 FROM `inheritance_paths`
WHERE `descendant` = NEW.`parent`
UNION ALL SELECT NEW.`it_id`, NEW.`it_id`, 0;
END; //
DELIMITER ;
DELIMITER //
CREATE TRIGGER `inheritance_tree_update` BEFORE UPDATE ON `inheritance_tree` FOR EACH ROW
BEGIN
-- From http://www.mysqlperformanceblog.com/2011/02/14/moving-subtrees-in-closure-table/
IF OLD.`parent` != NEW.`parent` THEN
-- Remove the node from its current parent
DELETE a FROM `inheritance_paths` AS a
JOIN `inheritance_paths` AS d ON a.`descendant` = d.`descendant`
LEFT JOIN `inheritance_paths` AS x
ON x.`ancestor` = d.`ancestor` AND x.`descendant` = a.`ancestor`
WHERE d.`ancestor` = OLD.`it_id` AND x.`ancestor` IS NULL;
-- Add the node to its new parent
INSERT `inheritance_paths` (`ancestor`, `descendant`, `len`)
SELECT supertree.`ancestor`, subtree.`descendant`, supertree.`len`+subtree.`len`+1
FROM `inheritance_paths` AS supertree JOIN `inheritance_paths` AS subtree
WHERE subtree.`ancestor` = OLD.`it_id`
AND supertree.`descendant` = NEW.`parent`;
END IF;
END; //
DELIMITER ;
CREATE TABLE `inheritance_paths` (
`ancestor` INT NOT NULL,
`descendant` INT NOT NULL,
`len` INT NOT NULL,
PRIMARY KEY (`ancestor`, `descendant`),
FOREIGN KEY (`ancestor`) REFERENCES inheritance_tree(`it_id`) ON DELETE CASCADE,
FOREIGN KEY (`descendant`) REFERENCES inheritance_tree(`it_id`) ON DELETE CASCADE
) ENGINE = INNODB;
INSERT INTO `inheritance_tree` VALUES(1, NULL, NULL, 'Animal');
INSERT INTO `inheritance_tree` VALUES(2, 1, 1, 'Mammal');
INSERT INTO `inheritance_tree` VALUES(3, 1, 2, 'Bird');
INSERT INTO `inheritance_tree` VALUES(4, 1, 3, 'Reptile');
INSERT INTO `inheritance_tree` VALUES(5, 2, 2, 'Feline');
INSERT INTO `inheritance_tree` VALUES(6, 2, 1, 'Bovine');
INSERT INTO `inheritance_tree` VALUES(7, 1, 3, 'Fish');
INSERT INTO `inheritance_tree` VALUES(8, 4, 1, 'Snake');
INSERT INTO `inheritance_tree` VALUES(9, 4, 2, 'Lizard');
INSERT INTO `inheritance_tree` VALUES(10, NULL, NULL, 'Machine');
INSERT INTO `inheritance_tree` VALUES(11, 10, 1, 'Automobile');
INSERT INTO `inheritance_tree` VALUES(12, 10, 2, 'OfficeMachine');
INSERT INTO `inheritance_tree` VALUES(13, 10, 3, 'Robot');
DELIMITER ;
CREATE TABLE `attributes` (
`a_id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'the unique identifier',
`attribute_name` varchar(255) DEFAULT NULL,
`attribute_type` int(11) NOT NULL,
PRIMARY KEY (`a_id`),
KEY `fk_attributes_attribute_type1_idx` (`attribute_type`),
CONSTRAINT `fk_attributes_attribute_type1_idx` FOREIGN KEY (`attribute_type`) REFERENCES `inheritance_tree` (`it_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=latin1;
INSERT INTO `attributes` VALUES(1, 'IsAlive', 1); -- Animal
INSERT INTO `attributes` VALUES(2, 'HasMaximumLifeSpan', 1); -- Animal
INSERT INTO `attributes` VALUES(3, 'IsNotAlive', 10); -- Machine
INSERT INTO `attributes` VALUES(4, 'HasIndeterminantLifeSpan', 10); -- Machine
INSERT INTO `attributes` VALUES(5, 'BreathesAir', 2); -- Mammal
INSERT INTO `attributes` VALUES(6, 'CanFly', 3); -- Bird
INSERT INTO `attributes` VALUES(7, 'HasFeathers', 3); -- Bird
INSERT INTO `attributes` VALUES(8, 'ColdBlooded', 4); -- Reptile
INSERT INTO `attributes` VALUES(9, 'HasFourLegs', 5); -- Feline
INSERT INTO `attributes` VALUES(10, 'HasFourLegs', 6); -- Bovine
INSERT INTO `attributes` VALUES(11, 'ColdBlooded', 7); -- Fish
INSERT INTO `attributes` VALUES(12, 'HasNoLegs', 8); -- Snake
INSERT INTO `attributes` VALUES(13, 'HasFourLegs', 9); -- Lizard
INSERT INTO `attributes` VALUES(14, 'ConsumesGasoline', 11); -- Automobile
INSERT INTO `attributes` VALUES(15, 'ConsumesElectricity', 12); -- OfficeMachine
INSERT INTO `attributes` VALUES(16, 'ConsumesElectricity', 13); -- Robot
INSERT INTO `attributes` VALUES(17, 'WarmBlooded', 2); -- Mammal
INSERT INTO `attributes` VALUES(18, 'WarmBlooded', 3); -- Bird
INSERT INTO `attributes` VALUES(19, 'BreathesWater', 7); -- Fish
INSERT INTO `attributes` VALUES(20, 'HasScales', 8); -- Snake
INSERT INTO `attributes` VALUES(21, 'HasSkin', 9); -- Lizard
Assumptions:
1.All entities in the taxonomy are uniquely named. This means there is one and only one sub-type called “Fish” anywhere in any of the taxonomy trees.
2.Attributes are not unique and may repeat
GOAL:
Given the input type “Lizard” I would like a single query that returns the following list of attribute records:
1, IsAlive, 1
2, HasMaximumLifeSpan, 1
5, BreathesAir, 2
8, ColdBlooded, 4
13, HasFourLegs, 9
21, HasSkin, 9
SELECT a.*
FROM inheritance_tree t
JOIN inheritance_paths p ON p.descendant = t.it_id
JOIN attributes a ON a.attribute_type = p.ancestor
WHERE t.name = 'Lizard'
See it on sqlfiddle.
Note that your example output includes a_id = 5 (BreathesAir), which is a property of it_id = 2 (Mammal), which is not in the Lizard's ancestry.