Hi I want to build a (Follow/Unfollow) button,
which should FOLLOW a page by 'adding' a record in a table {table columns are UserID and PageID} if the record doesn't exist,
or UNFOLLOW a page by 'removing' the record from the table if the record already exist
I know I can handle this in the PHP code by creating 2 queries, BUT I want to know if I can do it with 1 query using 'if statment' or something else!!
Thanks in advance.
In Oracle, you can use the MERGE statement with the DELETE clause, consider:
SQL> create table foo(userid number, pageid number, dummy number);
Table created
SQL> CREATE OR REPLACE PROCEDURE follow(p_userid NUMBER, p_pageid NUMBER) IS
2 BEGIN
3 MERGE INTO foo f
4 USING (SELECT p_userid userid, p_pageid pageid FROM dual) i
5 ON (f.userid = i.userid AND f.pageid = i.pageid)
6 WHEN MATCHED THEN
7 UPDATE SET f.dummy = NULL
8 DELETE WHERE 1 = 1
9 WHEN NOT MATCHED THEN
10 INSERT (userid, pageid) VALUES (i.userid, i.pageid);
11 END;
12 /
Procedure created
The dummy column is needed because the MERGE statement can't update the join column (in the ON clause) and the UPDATE clause is necessary. Running the statement twice will delete the row:
SQL> exec follow(1, 1);
PL/SQL procedure successfully completed
SQL> select * from foo;
USERID PAGEID DUMMY
---------- ---------- ----------
1 1
SQL> exec follow(1, 2);
PL/SQL procedure successfully completed
SQL> select * from foo;
USERID PAGEID DUMMY
---------- ---------- ----------
1 1
1 2
SQL> exec follow(1, 1);
PL/SQL procedure successfully completed
SQL> select * from foo;
USERID PAGEID DUMMY
---------- ---------- ----------
1 2
As far as I know, You can't do it directly. As a workarround, depending of how you lanuch the queries, you can launch two queries in the same sentence using a semicolon as a separator.
Of course, you can always use a stored procedure instead.
However, I don't think that this solutions are good practices. Keep your two queries in the same transaction and call it from the business layer. It will me more mantenible in the future.
Assuming that the Table that you want to add to or remove from is called User_Page.
IF EXISTS (SELECT * FROM User_Page WHERE USerID = #userID and PageID = #PageID)
BEGIN
DELETE FROM User_Page WHERE USerID = #userID and PageID = #PageID
END
ELSE
BEGIN
INSERT INTO User_Page (UserID, PageID) VALUES (#userID, #pageID)
END
Related
I need to create a trigger (after insert on one table) on MySQL, but the action needs to join 2 tables, for inserting into a third table. My script below returns no error, but the row is not inserted into the third table.
The first table (on which the after-insert trigger should work):
Z_TAXO
ID term_ID taxo_name
1 1 dept
2 2 staff
3 4 course
4 5 dept
The second table to be joined in the trigger:
Z_TERM
term_ID name
1 Engineering
2 Andy
4 Metallurgy
5 Business
6 Arts
The third table. If the Z_TAXO table has a new row with taxo_name = "dept", the row (joined with table Z_TERM) needs to be inserted into this table:
Z_DEPTS
ID dept_name
1 Engineering
4 Business
I created a trigger:
delimiter //
CREATE TRIGGER TRG_NEW_DEPT
AFTER INSERT ON Z_TAXO
FOR EACH ROW
BEGIN
DECLARE _dept_ID bigint(20);
DECLARE _dept_name varchar(200);
IF Z_TAXO.taxo_name = "DEPT" THEN
BEGIN
SELECT Z_TAXO.ID INTO _dept_ID FROM Z_TAXO, Z_TERM
WHERE Z_TAXO.ID = new.Z_TAXO.ID AND Z_TAXO.term_ID = Z_TERM.term_ID;
SELECT Z_TERM.name INTO _dept_name FROM Z_TERM, Z_TAXO
WHERE Z_TAXO.term_ID = Z_TERM.term_ID AND Z_TAXO.ID = new.Z_TAXO.ID;
INSERT INTO Z_DEPTS (ID, dept_name) VALUES (_dept_ID, _dept_name);
END;
END IF;
END//
delimiter ;
Then inserted a row to the Z_TAXO table:
INSERT INTO Z_TAXO (ID, term_ID, taxo_name) VALUES (5, 6, "dept");
Expecting to have this new row in table Z_DEPTS:
ID dept_name
5 Arts
But when I select * from Z_DEPTS, the result is still:
ID dept_name
1 Engineering
4 Business
What can be wrong? I can't modify the design of the tables, because they came from a wordpress Plugin. Thanks in advance!
Couple of comments about your code. 1) When using new. qualifiers you don't further qualify with the table name so new.z_taxo.id is invalid andd should simply be new.id 2) You don't need a begin..end block in a mysql if statement 3) if just doesn't make sense referring to the table z_taxo in your select stataments - a simple insert select will do.
try
drop trigger if exists trg_new_dept;
delimiter //
CREATE TRIGGER TRG_NEW_DEPT
AFTER INSERT ON Z_TAXO
FOR EACH ROW
BEGIN
INSERT INTO Z_DEPTS (ID, dept_name)
select term_id, name
from z_term
where term_id = new.term_id;
END//
delimiter ;
i want to retrieve a values from mysql table compare with three table using mysql query.
My Tables:
main_tbl:
id name email_id
--------------------
1 test test#gmail.com
Login_tbl:
login_id id username
---------------------
1 1 anu
2 1 ahalya
sublogin_tbl:
login_id id name emailid
------------------------
2 1 priya priya#gmail.com
If i enter a username as "ahalya" first it checks with the login_id exists in sublogin_tbl if value exists means it retrieve the values(name,emailid) from sublogin_tbl otherwise it returns the values from main_tbl.
Its not clear what you wanted exactly. Lets go through your need sentence by sentence
If i enter a username as "ahalya" first it checks with the login_id exists
'passing #username - ahalya
declare #intId as int;
Select #intId = (Select id from Login_tbl where username=#username )
if #intId >0 'login_id exists :retrieve the values(name,emailid) from sublogin_tbl
begin
Select name,emailid from sublogin_tbl where id=#intId
end
else 'otherwise it returns the values from main_tbl.
begin
'Here i don't know what exactly you want, we are searching by username and its only present in login table, put where in below as per your need !
select * from main_tbl
end
And if you are looking for result in single query then this can be used (But only if login_tbl id is compared with main_tbl id - else you have to make some changes):
Here is the Fiddle
select name,emailid from sublogin_tbl where login_id =
(select login_id from login_tbl where username='ahalya')
union
select name,email_id from main_tbl where id =
(select id from login_tbl where username='ahalya')
Limit 1
It will be better to create a stored procedure and inside the stored procedure put if else conditon to check if login id exists....
create procedure procedureName
(#userName nvarchar(50))
as
declare #counter int=0
set #counter=Select count(id)
from sublogin_tbl
where name=#userName
I have an auto increment column ID, and for some situation I wanted the other column to be equal to the primary key + 1 value
ID | other
1 | 2
2 | 3
3 | 4
4 | 123 (some situation, it is not always plus 1)
How can I achieve this?
Here's what I have tried
INSERT INTO table (`ID`,`other`) VALUES ('',(SELECT MAX(ID)+1 FROM table))
But that returns an error
You can't specify target table 'table' for update in FROM clause
Try Below query:
ALTER TABLE dbo.table ADD
Column AS ([ID]+1)
GO
It will definitely work
Using a normal AUTO_INCREMENT column as id, I cannot think of a way to do this in MySQL. Triggers, which otherwise would have been an option, don't work well with AUTO_INCREMENT columns.
The only way I see is to do two commands for an INSERT;
INSERT INTO bop (value) VALUES ('These values should be 1 and 2');
UPDATE bop SET other = id+1 WHERE id = LAST_INSERT_ID();
An SQLfiddle to test with.
The closest I'm getting to what you're looking for is to generate sequences separately from AUTO_INCREMENT using a function, and use that instead to generate the table id;
DELIMITER //
CREATE TABLE bop (
id INT UNIQUE,
other INT,
value VARCHAR(64)
)//
CREATE TABLE bop_seq ( seq INT ) // -- Sequence table
INSERT INTO bop_seq VALUES (1) // -- Start value
CREATE FUNCTION bop_nextval() RETURNS int
BEGIN
SET #tmp = (SELECT seq FROM bop_seq FOR UPDATE);
UPDATE bop_seq SET seq = seq + 1;
RETURN #tmp;
END//
CREATE TRIGGER bop_auto BEFORE INSERT ON bop
FOR EACH ROW
SET NEW.id = bop_nextval(), NEW.other=NEW.id + 1;
//
That'd let you do inserts and have it autonumber like you want. The FOR UPDATE should keep the sequence transaction safe, but I've not load tested so you may want to do that.
Another SQLfiddle.
I solved this by updating 2 times the DB..
I wanted to do +1 from 19 till ..
UPDATE `table` SET `id`=`id`+101 WHERE id <= 19
UPDATE `table` SET `id`=`id`-100 WHERE id <= 119 AND id >= 101
I saw this answer and i hope he is incorrect, just like someone was incorrect telling primary keys are on a column and I can't set it on multiple columns.
Here is my table
create table Users(id INT primary key AUTO_INCREMENT,
parent INT,
name TEXT NOT NULL,
FOREIGN KEY(parent)
REFERENCES Users(id)
);
+----+--------+---------+
| id | parent | name |
+----+--------+---------+
| 1 | NULL | root |
| 2 | 1 | one |
| 3 | 1 | 1down |
| 4 | 2 | one_a |
| 5 | 4 | one_a_b |
+----+--------+---------+
I'd like to select user id 2 and recurse so I get all its direct and indirect child (so id 4 and 5).
How do I write it in such a way this will work? I seen recursion in postgresql and sqlserver.
CREATE DEFINER = 'root'#'localhost'
PROCEDURE test.GetHierarchyUsers(IN StartKey INT)
BEGIN
-- prepare a hierarchy level variable
SET #hierlevel := 00000;
-- prepare a variable for total rows so we know when no more rows found
SET #lastRowCount := 0;
-- pre-drop temp table
DROP TABLE IF EXISTS MyHierarchy;
-- now, create it as the first level you want...
-- ie: a specific top level of all "no parent" entries
-- or parameterize the function and ask for a specific "ID".
-- add extra column as flag for next set of ID's to load into this.
CREATE TABLE MyHierarchy AS
SELECT U.ID
, U.Parent
, U.`name`
, 00 AS IDHierLevel
, 00 AS AlreadyProcessed
FROM
Users U
WHERE
U.ID = StartKey;
-- how many rows are we starting with at this tier level
-- START the cycle, only IF we found rows...
SET #lastRowCount := FOUND_ROWS();
-- we need to have a "key" for updates to be applied against,
-- otherwise our UPDATE statement will nag about an unsafe update command
CREATE INDEX MyHier_Idx1 ON MyHierarchy (IDHierLevel);
-- NOW, keep cycling through until we get no more records
WHILE #lastRowCount > 0
DO
UPDATE MyHierarchy
SET
AlreadyProcessed = 1
WHERE
IDHierLevel = #hierLevel;
-- NOW, load in all entries found from full-set NOT already processed
INSERT INTO MyHierarchy
SELECT DISTINCT U.ID
, U.Parent
, U.`name`
, #hierLevel + 1 AS IDHierLevel
, 0 AS AlreadyProcessed
FROM
MyHierarchy mh
JOIN Users U
ON mh.Parent = U.ID
WHERE
mh.IDHierLevel = #hierLevel;
-- preserve latest count of records accounted for from above query
-- now, how many acrual rows DID we insert from the select query
SET #lastRowCount := ROW_COUNT();
-- only mark the LOWER level we just joined against as processed,
-- and NOT the new records we just inserted
UPDATE MyHierarchy
SET
AlreadyProcessed = 1
WHERE
IDHierLevel = #hierLevel;
-- now, update the hierarchy level
SET #hierLevel := #hierLevel + 1;
END WHILE;
-- return the final set now
SELECT *
FROM
MyHierarchy;
-- and we can clean-up after the query of data has been selected / returned.
-- drop table if exists MyHierarchy;
END
It might appear cumbersome, but to use this, do
call GetHierarchyUsers( 5 );
(or whatever key ID you want to find UP the hierarchical tree for).
The premise is to start with the one KEY you are working with. Then, use that as a basis to join to the users table AGAIN, but based on the first entry's PARENT ID. Once found, update the temp table as to not try and join for that key again on the next cycle. Then keep going until no more "parent" ID keys can be found.
This will return the entire hierarchy of records up to the parent no matter how deep the nesting. However, if you only want the FINAL parent, you can use the #hierlevel variable to return only the latest one in the file added, or ORDER BY and LIMIT 1
I know there is probably better and more efficient answer above but this snippet gives a slightly different approach and provides both - ancestors and children.
The idea is to constantly insert relative rowIds into temporary table, then fetch a row to look for it's relatives, rinse repeat until all rows are processed. Query can be probably optimized to use only 1 temporary table.
Here is a working sqlfiddle example.
CREATE TABLE Users
(`id` int, `parent` int,`name` VARCHAR(10))//
INSERT INTO Users
(`id`, `parent`, `name`)
VALUES
(1, NULL, 'root'),
(2, 1, 'one'),
(3, 1, '1down'),
(4, 2, 'one_a'),
(5, 4, 'one_a_b')//
CREATE PROCEDURE getAncestors (in ParRowId int)
BEGIN
DECLARE tmp_parentId int;
CREATE TEMPORARY TABLE tmp (parentId INT NOT NULL);
CREATE TEMPORARY TABLE results (parentId INT NOT NULL);
INSERT INTO tmp SELECT ParRowId;
WHILE (SELECT COUNT(*) FROM tmp) > 0 DO
SET tmp_parentId = (SELECT MIN(parentId) FROM tmp);
DELETE FROM tmp WHERE parentId = tmp_parentId;
INSERT INTO results SELECT parent FROM Users WHERE id = tmp_parentId AND parent IS NOT NULL;
INSERT INTO tmp SELECT parent FROM Users WHERE id = tmp_parentId AND parent IS NOT NULL;
END WHILE;
SELECT * FROM Users WHERE id IN (SELECT * FROM results);
END//
CREATE PROCEDURE getChildren (in ParRowId int)
BEGIN
DECLARE tmp_childId int;
CREATE TEMPORARY TABLE tmp (childId INT NOT NULL);
CREATE TEMPORARY TABLE results (childId INT NOT NULL);
INSERT INTO tmp SELECT ParRowId;
WHILE (SELECT COUNT(*) FROM tmp) > 0 DO
SET tmp_childId = (SELECT MIN(childId) FROM tmp);
DELETE FROM tmp WHERE childId = tmp_childId;
INSERT INTO results SELECT id FROM Users WHERE parent = tmp_childId;
INSERT INTO tmp SELECT id FROM Users WHERE parent = tmp_childId;
END WHILE;
SELECT * FROM Users WHERE id IN (SELECT * FROM results);
END//
Usage:
CALL getChildren(2);
-- returns
id parent name
4 2 one_a
5 4 one_a_b
CALL getAncestors(5);
-- returns
id parent name
1 (null) root
2 1 one
4 2 one_a
In a previous question I asked how I could sum up a total based on some conditions: Count total on couple of conditions
Suppose I have a table like this:
id col1 col2 col3
1 a 1 k1
2 a 2 k2
3 a -3 k3
4 b 3 k4
Now, when I get id=1, I want to delete all the rows where col1=a.
When I get id=4, I want to delete all the rows where col1=b.
How would I do this in SQL?
I tried based upon previous answer:
DELETE FROM table WHERE (col1) IN (SELECT col1 FROM table WHERE id = '1')
But that gave me an error: #1093 - You can't specify target table 'table' for update in FROM clause
This has been many times on stackowerflow, you cannot UPDATE/DELETE table with data from nested select on the same table. There're two ways to do this:
Load all data before (for example via php, sql procedure)
Create temporary table like the one you're using, clone data and use temporary table to select items
i have another suggested solution for this. What if you create a STORED PROCEDURE for this problem?
like this:
DELIMITER $$
CREATE PROCEDURE `DeleteRec`(IN xxx varchar(5))
BEGIN
DECLARE oID varchar(5);
SET oID := (SELECT col1 FROM table WHERE id = '1');
DELETE FROM table WHERE col1 = oID;
END$$
DELIMITER ;
do this helps you?