I am trying to create a key/value table which fetches the instructor name and salary over 80000. I am not able to make a SELECT statement here. using Json here.
CREATE TABLE instructortest (
ID INT PRIMARY KEY,
info VARCHAR(max) NOT NULL
);
ALTER TABLE instructortest
ADD CONSTRAINT "valid JSON"
CHECK (ISJSON (info) = 1);
INSERT INTO instructortest
VALUES (78699,'{"name":"Pingr","Department":"Statistics","salary":"59303.62"}' )
select JSON_VALUE(info, '$.name') from instructortest
where ('$.salary') = 59303.62
You just need to use JSON_VALUE again in your where clause:
select JSON_VALUE(info, '$.name') from instructortest
where cast(JSON_VALUE(info, '$.salary') as decimal(18,2)) > 80000
Related
I was trying to pass parameter to my Query using MySQL as you can see in the code below. but it gives me No Records always .... any advise why?>
create table DemoTable
(
Id int,
FirstName varchar(20),
LastName varchar(20)
);
insert into DemoTable values(10,'Carol','Taylor');
select * from DemoTable;
set #myId:=10;
select *from DemoTable where Id=#myId;
0 ROWS SELECTED
I have created the following table in CQL:
CREATE TABLE new_table (
idRestaurant INT,
restaurant map<text,varchar>,
InspectionDate date,
ViolationCode VARCHAR,
ViolationDescription VARCHAR,
CriticalFlag VARCHAR, Score INT, GRADE VARCHAR,
PRIMARY KEY (InspectionDate )) ;
Then, I have inserted the data by jar, and I got the restaurant column value is like json/dictionary
select restarutant from new_table; is like the following result:
In normal SQL for selecting the json column's key value should be select json_col.key from table But that does not work for CQL, how can I select the JSON's key value as the column or for the WHERE condition filtering?
Thank you so much
Instead of using map, I would better to change the table's schema to following:
CREATE TABLE new_table (
idRestaurant INT,
restaurant_key text,
restaurant_value text,
InspectionDate date,
ViolationCode VARCHAR,
ViolationDescription VARCHAR,
CriticalFlag VARCHAR,
Score INT,
GRADE VARCHAR,
PRIMARY KEY (InspectionDate, restaurant_key )) ;
then you can select either individual row based on the restaurant_key with query like:
select * from new_table where idRestaurant = ? and restaurant_key = ?
or select everything for restaurant with:
select * from new_table where idRestaurant = ?
Hoping someone can help me with a mysql query
Here’s what I have:
I table with a column “networkname” that contains data like this:
“VLAN-338-Network1-A,VLAN-364-Network2-A,VLAN-988-Network3-A,VLAN-1051-Network4-A”
I need a MySQL query that will update that column with only the vlan numbers in ascending order, stripping out everything else. ie.
“338, 364, 988, 1051”
Thanks,
David
In this script, I create a procedure to loop through the networkname values and parse out the numbers to a separate table, and then update YourTable using a group_concat function. This assumes your networkname values follow the 'VLAN-XXX' pattern in your example where 'XXX' is the 3-4 digit number you want to extract. This also assumes each record has a unique ID.
CREATE PROCEDURE networkname_parser()
BEGIN
-- load test data
drop table if exists YourTable;
create table YourTable
(
ID int not null auto_increment,
networkname nvarchar(100),
primary key (ID)
);
insert into YourTable(networkname) values
('VLAN-338-Network1-A,VLAN-364-Network2-A,VLAN-988-Network3-A,VLAN-1051-Network4-A'),
('VLAN-231-Network1-A,VLAN-4567-Network2-A'),
('VLAN-9876-Network1-A,VLAN-321-Network2-A,VLAN-1678-Network3-A');
-- add commas to the end of networkname for parsing
update YourTable set networkname = concat(networkname,',');
-- parse networkname into related table
drop table if exists ParseYourString;
create table ParseYourString(ID int,NetworkNumbers int);
while (select count(*) from YourTable where networkname like 'VLAN-%') > 0
do
insert into ParseYourString
select ID,replace(substr(networkname,6,4),'-','')
from YourTable
where networkname like 'VLAN-%';
update YourTable
set networkname = right(networkname,char_length(networkname)-instr(networkname,','))
where networkname like 'VLAN-%';
end while;
-- update YourTable.networkname with NetworkNumbers
update YourTable t
inner join (select ID,group_concat(networknumbers order by networknumbers asc) as networknumbers
from ParseYourString
group by ID) n
on n.ID = t.ID
set t.networkname = n.networknumbers;
END//
Call to procedure and select the results:
call networkname_parser();
select * from YourTable;
SQL Fiddle: http://www.sqlfiddle.com/#!2/01c77/1
INSERT INTO `dictionary2` (word,verb)
SELECT SUBSTRING(word FROM 2)
FROM `dictionary1`
WHERE `dictionary1`.word LIKE "w%"
I have two tables, dictionary1(word) and dictionary2(word,verb).
I would like to insert into dictionary2, values from dictionary1, where word starts with 'w' and the value is not present in dictionary2.word.
In the same insert I would like to set the value of dictionary2.verb to 1.
You could use this:
INSERT INTO dictionary2 (word, verb)
SELECT dictionary1.word, 1
FROM
dictionary1 LEFT JOIN dictionary2
ON dictionary1.word = dictionary2.word
WHERE
dictionary1.word LIKE 'w%'
AND dictionary2.word IS NULL
Please see fiddle here.
Try this:
SQL Query:
INSERT INTO `dictionary2` (word,verb)
SELECT word, (select #number :=1)
FROM `dictionary1`
WHERE `dictionary1`.word LIKE "t%"
AND `dictionary1`.word NOT IN(SELECT word FROM dictionary2);
Sample data:
CREATE TABLE `dictionary2` (
ID INT AUTO_INCREMENT PRIMARY KEY,
word VARCHAR (50),
verb VARCHAR (50)
);
CREATE TABLE `dictionary1` (
ID INT AUTO_INCREMENT PRIMARY KEY,
word VARCHAR (50)
);
INSERT INTO `dictionary2`(word,verb)
VALUES
('test','test'),
('test1','test1');
INSERT INTO `dictionary1`(word) VALUES('test'),('testing1');
B.T.W. you can safely change LIKE "t%" into LIKE "w%" ;-)
SQL FIDDLE DEMO
Using only MySQL, I'm seeing if it's possible run an insert statement ONLY if the table is new. I successfully created a user variable to see if the table exists. The problem is that you can't use "WHERE" along with an insert statement. Any ideas on how to get this working?
// See if the "country" table exists -- saving the result to a variable
SELECT
#table_exists := COUNT(*)
FROM
information_schema.TABLES
WHERE
TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'country';
// Create the table if it doesn't exist
CREATE TABLE IF NOT EXISTS country (
id INT unsigned auto_increment primary key,
name VARCHAR(64)
);
// Insert data into the table if #table_exists > 0
INSERT INTO country (name) VALUES ('Afghanistan'),('Aland Islands') WHERE 0 < #table_exists;
IF #TableExists > 0 THEN
BEGIN
INSERT INTO country (name) VALUES ('Afghanistan'),('Aland Islands');
END
Use an if statement instead of the where clause:
http://dev.mysql.com/doc/refman/5.0/en/if-statement.html