This question already has answers here:
SQL query to find a list of city names that dont start with vowels
(26 answers)
Closed 26 days ago.
create table test(
id int,
name char(10)
);
insert into test values(1,'sukesh');
insert into test values(2,'ramesh');
insert into test values(3,'adkjdf');
insert into test values(4,'eeuf');
insert into test values(5,'hdkjdf');
insert into test values(6,'nhkjdf');
insert into test values(7,'pdkjdf');
insert into test values(8,'adkjdf');
insert into test values(9,'oeruw');
insert into test values(10,'iblesjf');
SELECT * FROM test
WHERE name LIKE '[!aeiou]%';
Here is what i tried but it out put is showing as
Program did not output anything!
create table test(
id int,
name char(10)
);
insert into test values(1,'sukesh'),(2,'ramesh'),(3,'adkjdf'),
(4,'eeuf'),(5,'hdkjdf'),(6,'nhkjdf'),(7,'pdkjdf')
SELECT * FROM test
WHERE name NOT LIKE '[!aeiou]%'; -- Put 'NOT' CLAUSE BEFORE 'LIKE'
You can use the NOT condition with RLIKE
SELECT * FROM test
WHERE name NOT RLIKE '^[aeiouAEIOU].*$'
Related
there are many ways to do the connect by prior with mysql5,
(I have known mysql8 support this query,however, I'm using mysql5),
I had known that I can use find_in_set to realize the connect by prior,
but how can I realize order siblings by just as oracle did.
for example,
here is the test data
e,my real data is that, the id is guid just like
62CF6FFEDE064B00ADFF43ED0E6EFDDB
create table test(id varchar(10),ordernumber decimal,pid varchar(10));
insert into test values('62CF6',2,'');
insert into test values('44DF8',3,'D13A3');
insert into test values('DD43F',5,'62CF6');
insert into test values('E7832',2,'53FAA');
insert into test values('48728',4,'D13A3');
insert into test values('53FAA',4,'62CF6');
insert into test values('9038A',1,'53FAA');
insert into test values('F5C7E',2,'D13A3');
insert into test values('D13A3',1,'');
in oracle i can use
starts with pid is null connect by prior id=pid order siblings by ordernumber asd
to get this result
id ordernumber pid
D13A3 1
F5C7E 2 D13A3
44DF8 3 D13A3
48728 4 D13A3
62CF6 2
53FAA 4 62CF6
9038A 1 53FAA
E7832 2 53FAA
DD43F 5 62CF6
since the level is unknown,I can only use the procedure like
just example code not real runnable.
create procedure test_p(in p varchar(100))
begin
declare aa varchar;
declare unit_cur cursor for select id from table where pid=p;
open unit_cur
fetch unit_cur into aa
call test_p(aa);
then if I have millions of tables to handle, how can i make only one procedure to the the similar thing?
SELECT *
FROM test
ORDER BY CASE WHEN pid = ''
THEN id
ELSE pid
END,
ordernumber;
I had changed the data,my true id is guid – JokerSora
SELECT t1.*
FROM test t1
LEFT JOIN test t2 ON t1.pid = t2.id
ORDER BY CASE WHEN t1.pid = ''
THEN t1.ordernumber
ELSE t2.ordernumber
END,
t1.ordernumber;
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=e3c01067da653f7ae5ce5c969d0bd57f
This question already has answers here:
MySQL LAST_INSERT_ID() used with multiple records INSERT statement
(3 answers)
Closed 3 years ago.
Have:
1. create DB
2. create Table
3. insert 3 rows
4. select LAST_INSERT_ID()
Here test code:
DROP DATABASE IF EXISTS TEST;
CREATE DATABASE TEST;
USE TEST;
CREATE TABLE test (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
age INT
);
INSERT INTO test (age) VALUES (1), (2), (3);
SELECT LAST_INSERT_ID();
Why LAST_INSERT_ID() return 1 ?
Excepted: 3
How to get valid LAST_INSERT_ID() ?
The MySQL documentation clearly explains this behavior:
With no argument, LAST_INSERT_ID() returns a BIGINT UNSIGNED (64-bit) value representing the first (emphasis mine) automatically generated value successfully inserted for an AUTO_INCREMENT column as a result of the most recently executed INSERT statement. The value of LAST_INSERT_ID() remains unchanged if no rows are successfully inserted.
The first value generated by the auto increment sequence in the insert was 1, not 2 or 3, so the value 1 gets returned.
I think the confusion you have is around the name LAST_INSERT_ID. The "last" part refers to the most recent insert statement, not the most recent id value within that insert.
I have two identical tables bar one column, (one live, one test)
The test table has an extra not null Column called "MatchOrderNo"
I'm trying to extract data from Live to test, how can i populate this not Null for all rows?
basically my current statement is
INSERT INTO test SELECT * FROM live;
Depends what do you want to insert to the NOT NULL column? For a constant value :
INSERT INTO test
SELECT t.* , 'Val_For_NotNull_Col' FROM live t
You can use a Default value like:
INSERT INTO test SELECT live.col1, live.co2, '1' FROM live;
I have tried a scenario which needs to store last 5 months of data in netezza. below is the code which I've tried. But after that I got a wired thought and struck there to solve that.
create table test(yermon int,number int);
insert into test values(201607,1);
insert into test values(201606,2);
insert into test values(201605,3);
insert into test values(201604,4);
insert into test values(201603,5);
And then if I insert a new record with new yearmonth my below logic is working.
delete from test where yermon in (select min(yermon) from test where no=5);
update test set no = no + 1;
insert into test values(201608,1);
But if I run the same again twice. 201607 will go and store twice and 201603 ll delete.
I need a delete and update statement to include this situation as well. Can some one please help me on this.
Thanks in advance!
Can You try this?
CREATE TABLE `test` (
year_month int(6),
number int(11),
PRIMARY KEY year_month
);
I've found MERGE INTO statement, maybe I'm wrong, but test it:
MERGE INTO test AS dst USING test AS src ON (src.year_month=201608)
WHEN MATCHED THEN UPDATE SET dst.number = 1
WHEN NOT MATCHED THEN INSERT VALUES(201608, 1);
I have a View that has several fields.
When i INSERT INTO a view I run a function based on INSERT parametrs. The function returns a value.
How can I retrieve The value from rule?
INSERT RETURNING Gives me:
ERROR: cannot perform INSERT RETURNING on relation "full_subntes"
HINT: You need an unconditional ON INSERT DO INSTEAD rule with a RETURNING clause.
Example:
CREATE TABLE test (
a VARCAHR primary key,
b VARCHAR,
);
CREATE VIEW test_v AS SELECT * FROM test;
CREATE OR REPLACE RULE Test_v_Insert AS ON INSERT TO Test_v
DO INSTEAD (
SELECT myFunction('param');
);
INSERT INTO test_v(a, b) VALUES ('a', 'b') RETURNING a, b;
Then I get an error described above.
Here is an example.
First, we create a test table:
CREATE TABLE test (a integer, b varchar, primary key (a));
Then, we create a view:
CREATE OR REPLACE VIEW test_view AS SELECT * FROM test;
Next, the update rule is created:
CREATE OR REPLACE RULE rl_test_view_update AS
ON UPDATE TO test_view DO INSTEAD
UPDATE test SET a = NEW.a, b = NEW.b
WHERE test.a = old.a AND test.b = old.b;
And finally, here is the insert rule:
CREATE OR REPLACE RULE rl_test_view_insert AS
ON INSERT TO test_view DO INSTEAD
INSERT INTO test VALUES (NEW.a, NEW.b)
RETURNING test.*;
Now you can insert some test data:
INSERT INTO test_view (a, b) VALUES (1,'John Doe') RETURNING a;
and check the tuples inserted:
SELECT * FROM test_view;
In order to update a view in Postgres, you need to define a rule telling it to update the base table instead. It doesn't sound like you've created a rule yet. http://www.postgresql.org/docs/current/interactive/rules-update.html