I am very new to MySql and I am having a lot of trouble with the syntax. I am looking for a query that will do the following.
if string 'abc' is present in any row of column 'X', then insert string 'def' into column 'Y', only in the same rows that contain 'abc' in column 'X'.
I have tried a few different statements but i continue to receive syntax errors. Hopefully I have explained this clearly enough in pseudo
Try to use this query (plz change tablename, column X,Y):
update tablename set Y='def' where X like '%abc%';
What I have is a text column in Postgresql which I want to convert to JSONB column.
What I have tried is this:
CREATE TABLE test (id serial, sec text, name text);
INSERT INTO test (id, sec, name) VALUES (1,'{"gender":"male","sections":{"a":1,"b":2}}','subject');
ALTER TABLE test ALTER COLUMN sec TYPE JSONB USING sec::JSONB;
This did convert the text column to jsonb.
However, if I try to query:
SELECT sec->>'sections'->>'a' FROM test
I get an error.
I see the conversion is done only at one level(i.e: sec->>'sections' works fine).
The query SELECT pg_typeof(name->>'sections') from test; gives me column type as text.
Is there a way I can convert the text to jsonb entirely, such that I can query SELECT sec->>'sections'->>'a' FROM test; successfully?
I don't want to convert the text to json in the query like below, as I need to create index on 'a' later.
select (sec->>'sections')::json->>'a' from test;
The operator ->> gives a text as a result. Use -> if you want jsonb:
select
pg_typeof(sec->>'sections') a,
pg_typeof(sec->'sections') b
from test;
a | b
------+-------
text | jsonb
(1 row)
Use:
select sec->'sections'->>'a'
from test;
Or better, yet, use the operator #>>:
SELECT sec #>> '{sections,a}' FROM test;
And to use this in an expression index you need extra parentheses:
CREATE INDEX foo ON test ((sec #>> '{sections,a}'));
Make sure to use a matching expression (without parentheses) in queries to allow index usage.
I am running some SQL queries from command line as follows:
cat my_query.sql | mysql --defaults-file=my_conf.cnf
I need to print column names whether the query returns any data or not.
Currently when there is no data to return, i.e when query returns an empty result set this command does not print anything.
For example my query is:
-- id=-1 doesn't exist
SELECT col1, col2 FROM table WHERE id=-1
I need this query to return
col1 col2
Instead it returns nothing.
Is it possible to do that using purely mysql and standard unix commands?
Thanks.
Adding a UNION ALL to a SELECT with "dummy"/blank data might work:
SELECT col1, col2 FROM table WHERE id=-1
UNION ALL
SELECT '', '' -- OR 0, 0 whatever is appropriate
;
I don't run queries from the command line, so this is assuming it normally would give you the column names if you had at least one row in the results.
In my SQL tables there are rows where columnX has empty value (""). Now i want them i queried to select them and then delete them.
Query like:
tables has empty rows
Delete empty rows
How can i do this. Any idea
Depending on what exactly you mean by "empty" rows:
delete from yourTable where column1 is null
will delete where column1 has a null value. If you mean where multiple columns have nulls, it's just a matter of adding more conditions to the where clause:
delete from yourTable where column1 is null and column2 is null and column3 is null
If by empty you mean "has spaces in a text field or the field is empty" you can use some of the builtin functions to find them for example:
delete from yourTable where trim(column1)=''
which would find a row in the table where column1 only has white space in it and so on.
You might want to have a read of this article that I wrote on SQL, join and the like - it has got a fair bit in it about selecting the right rows from the table - and in your case, replace the select.... from where... with a delete from where...
Having said all that, I would really wonder why you are inserting data into your table that you don't want in it?
You can check each field for null or the empty string like this:
DELETE FROM table WHERE (column1 IS NULL OR column1 = '') AND (column2 IS NULL OR column2 = '')
Just add the rest of your columns to the WHERE clause.
Simple : delete from Test_table where c1 is null,....and cN is null
Ok Try this, i hope u'll find your solution
What you need is, first get empty rows
Select * From table_name Where column_name = "";
Then Delete the empty rows
Delete From table_name Where column_name = "";
or don't write the select query only write the delete query
I hope this solve your problem
I'm trying to update a field of my table with the CONCAT of the some fields of the same table.
Whith this
UPDATE tabex SET field1=CONCAT(tabex.a1,', ',tabex.a2,', ',tabex.a3,', ',tabex.a4,', ',tabex.a5,', ',tabex.a6,', 'tabex.a7,', ',tabex.a8,', ',tabex.a9 );
This query has 0 rows affected and no errors.
With this other query
UPDATE tabex SET field1=CONCAT_WS(tabex.a1,', ',tabex.a2,', ',tabex.a3,', ',tabex.a4,', ',tabex.a5,', ',tabex.a6,', 'tabex.a7,', ',tabex.a8,', ',tabex.a9 );
If the content of some of a(n) fields is NULL mysql puts a copy of the previous result
Someone can help me?
When this query
UPDATE tabex SET field1=CONCAT(tabex.a1,', ',tabex.a2,', ',tabex.a3,', ',tabex.a4,', ',tabex.a5,', ',tabex.a6,', 'tabex.a7,', ',tabex.a8,', ',tabex.a9 );
doesn't affect a row, the only explanation would be, that the table is empty. It would update every row in the table. But if one of the columns is NULL, your field1 column will also be NULL.
To avoid that, you have to use the COALESCE() function. This function returns the first of its parameters which is not NULL.
UPDATE tabex SET field1=CONCAT(COALESCE(tabex.a1, ''),', ',...);
On a sidenote I have to ask, why you want to do this. Comma separated values in columns are a bad idea most of the times.
And finally, your query using CONCAT_WS() is wrong. The _WS in the function name is short for "with separator", so the first parameter is the separator which then is placed between the other parameters of the function. So you should write it like this:
UPDATE tabex SET field1=CONCAT_WS(',', tabex.a1, tabex.a2, tabex.a3,...);
Another advantage of the CONCAT_WS() function is, that it ignores NULL values. Read more about the two functions in the manual.