I have an AFTER INSERT-trigger that looks like this.
BEGIN
SET #newcompany = NEW.company_id;
IF NEW.company_id is not null THEN
SET #my_result = (SELECT count(*) FROM wp_wysija_user u
LEFT JOIN wp_pods_company c ON c.id = u.company_id
LEFT JOIN wp_podsrel rel ON c.id = rel.item_id
WHERE rel.field_id=3384 AND rel.related_item_id=5 AND u.company_id = #newcompany);
INSERT INTO wp_wysija_user_list(list_id, user_id, sub_date)
VALUES (5, NEW.user_id, UNIX_TIMESTAMP());
IF #my_result > 0 THEN
INSERT INTO wp_wysija_user_list(list_id, user_id, sub_date)
VALUES (7, NEW.user_id, UNIX_TIMESTAMP());
END IF;
END IF;
END
My problem is that #my_result is always 0. If I run the query outside the trigger I get the right count. At first I thought that the variable #newcompany didnĀ“t get a value. But I have checked that.
I need help here!
Don't understand why you use LEFT joins????
BEGIN
SET #newcompany = NEW.company_id;
IF NEW.company_id is not null THEN
SET #my_result = (SELECT count(*) FROM wp_wysija_user u
LEFT JOIN wp_pods_company c ON c.id = u.company_id
LEFT JOIN wp_podsrel rel ON c.id = rel.item_id and rel.field_id=3384 AND
rel.related_item_id=5
WHERE u.company_id = #newcompany);
INSERT INTO wp_wysija_user_list(list_id, user_id, sub_date)
VALUES (5, NEW.user_id, UNIX_TIMESTAMP());
IF #my_result > 0 THEN
INSERT INTO wp_wysija_user_list(list_id, user_id, sub_date)
VALUES (7, NEW.user_id, UNIX_TIMESTAMP());
END IF;
END IF;
END
Related
Can't find the same questions as mine I'm hoping you guys can help me.
I have this query for my SP what I need to do is check if the company Id is the same based on the parameter given by the user.
I have two parameters which are paramOrgA and paramOrgB which will be supplied by the user.
What I did was repeat the same query and set the returned value in two variables which is
orgAId and orgBId then use the IF statement to compare the value.
is there another way to accomplish this or should I say optimized way to do this?
Below is the query I wrote.
BEGIN
declare orgAId int;
declare orgBId int;
declare orgStatus bool;
SET orgAId = (SELECT c.Id
FROM Members as `m`
INNER JOIN Company as `c`
ON m.CompanyId = c.Id
WHERE m.Id = paramOrgA);
SET orgBId = (SELECT c.Id
FROM Members as `m`
INNER JOIN Company as `c`
ON m.CompanyId = c.Id
WHERE m.Id = paramOrgB);
IF (orgAId = orgBId) THEN
set orgStatus = true;
ELSE
set orgStatus = false;
END IF;
select orgStatus as 'CompanyStatus';
END IF;
END
One query will do
SELECT count(distinct c.Id) > 1 as 'CompanyStatus'
FROM Members as `m`
INNER JOIN Company as `c` ON m.CompanyId = c.Id
WHERE m.Id IN (paramOrgA, paramOrgB)
Even fewer keystrokes:
SELECT COUNT(DISTINCT CompanyId) > 1 as 'CompanyStatus'
FROM Members
WHERE Id IN (paramOrgA, paramOrgB)
I have this trigger
DELIMITER ;
DELIMITER $$
CREATE TRIGGER when_auction_ends
AFTER UPDATE ON Auctions
FOR EACH ROW
BEGIN
SELECT A.vin, A.status, A.winner, A.minprice, B.amount, B.buyer INTO #vin, #stat, #winner, #minprice, #amount, #buyer
FROM Auctions A, Bids B WHERE A.status = 'Inactive' AND A.winner = null AND A.vin = B.vin AND B.amount = (SELECT MAX(amount) FROM Bids B WHERE B.vin = A.vin AS
IF #amount < #minprice THEN
UPDATE Auctions
SET A.winner = 'NONE'
WHERE A.vin = B.vin;
ELSEIF #amount >= #minprice THEN
UPDATE Auctions
SET A.winner = B.buyer
WHERE A.vin = B.vin;
END IF ;
END$$
DELIMITER ;
But when it activates, the values are not updated. I am thinking that its because mysql does not know what A.winner is in "SET A.winner". I tried looking online on how to use OLD and NEW but did not have any luck.
I am working on creating a new query for inserting values in to a table which pulls values from the other table
INSERT INTO user (EmailAddress, InternalPhone, ExternalPhone, CreatedBy, DateCreated,roll_key,user_key )
VALUES ( NULL, NULL, NULL, 1, 'dba', Now(),
(select r.roll_key from user u
join work wrk on u.work_key = wrk.Work_key
join roll r on r.Name = 'Ext'
where u.Name = 'test'),
(select u.user_key from user u
join work wrk on u.work_key = wrk.Work_key
join roll r on r.Name = 'Ext'
where u.Name = 'test'))
How can I simply this query instead of having the select statements in to the values.
You could use a single select and assign the fixed value to pseudo column
INSERT INTO user (EmailAddress, InternalPhone, ExternalPhone, CreatedBy, DateCreated,roll_key,user_key )
select NULL, NULL, 1, 'dba', Now(), r.roll_key, u.user_key
from user u
join work wrk on u.work_key = wrk.Work_key
join roll r on r.Name = 'Ext'
where u.Name = 'test'
I have the below code.
There is one which is one part which is "ISNULL(PO101_EXT_COST, 0)*MM01.MM021_RATE AS Amount, "
I will like to change to a if else statement.
When "MM01.MM021_RATE" = '1' then PO101_EXT_COST * MM021_FOREIGN_RATE
When "MM01.MM021_RATE" is not = '1' then PO101_EXT_COST * MM021_RATE
How can i change my code below?
SELECT
CASE
When MM01.MM021_RATE = 1
Then ISNULL(PO101_EXT_COST, 0)*MM01.MM021_FOREIGN_RATE
Else ISNULL(PO101_EXT_COST, 0)*MM01.MM021_RATE
END AS Amt END AS Amount,
ISNULL(IN100_INV_CODE,'') AS [Item Code],
ISNULL(IN006_DESC,'') AS Major,
ISNULL(IN007_DESC,'') AS Minor,
ISNULL(IN100_DESC,'') AS Item,
ISNULL(PO100_PO_REQ, 0) AS [PO #],
ISNULL(PO100_EVT_ID, 0) AS [Ev ID],
ISNULL(EV200_EVT_DESC, '') AS [Ev Desc],
ISNULL(PO101_EXT_COST, 0) AS OriginalAmt,
ISNULL(PO101_PHASE, '') AS Phase,
ISNULL(PO101_STATUS, '') AS Status,
ISNULL(EV870_ACCT_SECURITY,'') AS [ARCtl],
ISNULL(PO102_ACCOUNT,'') AS [GL Account],
MM01.MM021_EFFECTIVE_DATE,
MM01.MM021_RATE,
MM01.MM021_FOREIGN_RATE,
(PO101_ORG_CODE) AS ORG,
(PO101_CURRENCY) AS Currency, (PO100_APPROVAL) AS Approval
FROM PO101_ORD_DTL
LEFT OUTER JOIN PO100_ORDERS ON
PO100_ORG_CODE = PO101_ORG_CODE AND
PO100_PO_REQ = PO101_PO_REQ
LEFT JOIN PO102_DISTR ON
PO102_ORG_CODE = PO101_ORG_CODE AND
PO102_PO_REQ = PO101_PO_REQ AND
PO102_PO_REQ_SEQ = PO101_ORD_SEQ
LEFT OUTER JOIN EV200_EVENT_MASTER ON
PO100_ORG_CODE = EV200_ORG_CODE AND
PO100_EVT_ID = EV200_EVT_ID
LEFT OUTER JOIN MM021_CURRENCY_RATES MM01 ON
PO101_CURRENCY = MM01.MM021_CODE
INNER JOIN
(SELECT MM021_CODE, MAX(MM021_EFFECTIVE_DATE) AS EFFECTIVE_DATE
FROM MM021_CURRENCY_RATES
WHERE MM021_FOREIGN_RATE > 0 AND MM021_TO_CODE = '***'
GROUP BY MM021_CODE) MM02 ON
MM01.MM021_CODE = MM02.MM021_CODE AND
MM01.MM021_EFFECTIVE_DATE = MM02.EFFECTIVE_DATE
LEFT OUTER JOIN IN100_INV_MASTER ON
PO101_ITEM = IN100_INV_CODE AND
PO101_ORG_CODE = IN100_ORG_CODE
LEFT OUTER JOIN IN007_MINOR_GROUP ON
IN100_ORG_CODE = IN007_ORG_CODE AND
IN100_MINOR = IN007_MINOR AND
IN100_MAJOR = IN007_MAJOR
LEFT JOIN IN006_MAJOR_GROUP ON
IN006_ORG_CODE = IN007_ORG_CODE AND
IN006_MAJOR = IN007_MAJOR
LEFT JOIN EV870_ACCT_MASTER ON
PO101_BILLTO=EV870_ACCT_CODE AND
PO101_ORG_CODE=EV870_ORG_CODE
WHERE
(PO101_ORG_CODE = '40' ) AND
(EV200_EVT_DESC = 'Tyrexpo India 2016') AND
(ISNULL(PO100_APPROVAL,'') IN ('A9')) AND
(ISNULL(PO101_STATUS, '') IN ('O','C','H')) AND
(ISNULL(PO101_PHASE, '') IN ('1')) AND
(PO101_TAX_RES_TYPE <> '8APGST')
Thanks!
/*
DROP TABLE A;
DROP TABLE B;
CREATE TEMPORARY TABLE A (ID INT, rateid int,cost int);
CREATE TEMPORARY TABLE B(ID INT,rate int,foreignrate int);
TRUNCATE TABLE A;
INSERT INTO A VALUES(1,1,1),(2,1,null),(3,2,3),(4,2,null),(5,0,null),(6,7,null) ,(7,1,null) ,(8,0,null) ;
TRUNCATE TABLE B;
INSERT INTO B VALUES(1,10,20),(2,30,40);
*/
#SELECT A.* FROM A;
#SELECT B.* FROM B;
SELECT A.*,
case
when B.id = 1 then
case
when A.cost is not null and B.foreignrate is not null then A.cost * B.foreignrate
else 0
end
when B.id is not null then
case
when A.cost is not null and B.rate is not null then A.ID * B.rate
else 0
end
else 0
END AS AMT
FROM A
LEFT OUTER join B on B.ID = A.rateid
We have two table with this structures:
table_user : id,name
table_user_groups: user_id, group_id
some users has no group so they have no entry in table_user_groups.
I want to determine these users and insert a default group_id for them.
I think I have to use an INSERT query with a JOIN condition.
any suggestion.
thanks in advance.
The Select statement to get the users you want is that and below that is the cursor for the insert.
SELECT A.id FROM table_user A
LEFT OUTER JOIN table_user_groups B ON A.id = B.user_id
WHERE B.user_id = NULL
DECLARE #userid int
DECLARE cursor CURSOR FOR
SELECT A.id FROM table_user A
LEFT OUTER JOIN table_user_groups B ON A.id = B.user_id
WHERE B.user_id = NULL
OPEN cursor
FETCH NEXT FROM cursor
INTO #userid
WHILE ##FETCH_STATUS = 0
BEGIN
Insert into table_user_groups(user_id,group_id) VALUES (userid, 'DEFAULTUSERID')
END
CLOSE vendor_cursor;
DEALLOCATE vendor_cursor;
Try something like this:
> INSERT INTO table_user_groups(user_id, group_id)
SELECT tu.id, [DEFAULT ID]
FROM table_user tu
LEFT JOIN table_user_groups tug ON tug.user_id = tu.id
WHERE tug.id IS NULL;
Where [DEFAULT ID] is the value what you want to use as default for this users.
UNTESTED
use this :
INSERT INTO table_user_group
(SELECT id,'yourdefaultID' FROM table_user WHERE id
NOT IN (SELECT user_id FROM table_user_groups))