I have looked through questions here and have not been able to find exactly what I am looking for.
How would I create a query in mysql that would return missing data in field Name in database Demo as a string (such as 'Blank') instead of NULL?
I really appreciate your help!
SELECT IFNULL(fieldname, 'Blank') FROM tablename
or
SELECT COALESCE(fieldname, 'Blank') FROM tablename
Yes, in MySQL you can use IFNULL, as in:
SELECT IFNULL(fieldname, 'Blank') as fieldname, ...
I'm not really sure what you're aiming at, but maybe you look for something like this?
SELECT IF(ISNULL(a.yourfield),'Blank', a.yourfield)
FROM yourtable A;
ISNULL() returns 1 if the argument is NULL, otherwise it returns 0.
Related
I have a Table A
id
something
jsonModel
jsonModel could look like this
{
"text":"foo",
"subkey1":{
"entity":{
"name":"foo",
"customId":"59d61ffcf5bcb11f250d73275a252b62624eac000180ab59"
}
}
}
I'm trying to select a row from table A when a certain ID is contained in the the jsonModel.
I have tried this but it does not work saying I'm not allowed to use wildcards in the expression
SELECT
*
FROM A
WHERE json_contains(jsonModel,'59d61ffcf5bcb11f250d73275a252b62624eac000180ab59', $**.customId)
Is there a way to achieve this knowing that I do not know where in the model customId can be?
EDIT:
I ended up writing this which works but I don't know which solution between this or wchiquito is best
SELECT *
from A WHERE
JSON_CONTAINS(JSON_EXTRACT(jsonModel, '$**.customId'), '"59d61ffcf5bcb11f250d73275a252b62624eac000180ab59"');
Try:
SELECT
`id`,
`jsonModel`
FROM
`A`
WHERE
JSON_SEARCH(
`jsonModel`,
'one',
'59d61ffcf5bcb11f250d73275a252b62624eac000180ab59',
NULL,
'$**.customId'
) IS NOT NULL;
See db-fiddle.
Even tough its not correct or best approach but it works,
SELECT * from `A` where `jsonModel` like '%customId%59d61ffcf5bcb11f250d73275a252b62624eac000180ab59%'
What is an operator in MySQL queries that covers AND and OR. So if I wanted to select a row where value1=lol OR value2=loly or both do.
this will look for one of the values or both.
SELECT * FROM my_table WHERE your_column IN ( 'lol','loly' )
With a query with the OR it'll work:
SELECT * FROM my_table WHERE my_field = 'lol' OR my_field = 'loly'
it'll give you all the results with the first or the second
As an extra, I think you could be looking for WHERE with regular expressions: Check this manual page: https://dev.mysql.com/doc/refman/5.1/en/regexp.html
Sorry, this wasn't a very good question.
I now understand that the OR operator will work with either condition met, or both.
AND only works with both.
XOR works with either one but not both.
Credit goes to the comments replying to my question.
I use this code in MSSQL:
SELECT IDENT_CURRENT('customers');
When I try it in MySQL it doesn't work. I looked for answers on the net, but I could not find anything that worked for me. What's the MySQL equivalent for the above TSQL?
I think you are looking for this:
SELECT LAST_INSERT_ID('customers');
But LAST_INSERT_ID() not in all cases true, so better use:
SELECT MAX('id') FROM customers;
in MySQL, you can use LAST_INSERT_ID(expr)
LAST_INSERT_ID(expr)
I donĀ“t know if you still need it but it may help someone. :)
I was facing the same problem of needing to get the next id (auto_increment number) from my table without having to add a new row and without using MAX() or LAST_INSERT_ID() because it would only return the last visible record and not the real next auto_increment.
The solution I found was to get the auto_increment from the table_schema, like this:
SELECT AUTO_INCREMENT
FROM information_schema.TABLES
WHERE (TABLE_NAME = 'your_table')
Hope it help someone, like it helped me.
*Sorry the bad english, I'm from Brasil.
SELECT LAST_INSERT_ID();
To learn more about this function: http://dev.mysql.com/doc/refman/5.6/en/information-functions.html#function_last-insert-id
In order for this to work as intended, the 'ID' column must be set to "UNIQUE".
If you are using some sort of Identity(incrementing IDs) column the following should work;
Select top 1 ID from TBLNAME
order by ID DESC
I ran a query that resulted in the string '1,2,3,4'.
How can I run a second query that treats that string as a list of numbers. So I'll be able to do:
select * from tbl where name not in (1,2,3,4)
I would like an answer in pure MySQL.
Well first of all, this usually means that your database structure is not good; you should normalize your database.
However, you can do what you want, with the FIND_IN_SET function:
SELECT * FROM tbl WHERE NOT FIND_IN_SET(name, '1,2,3,4')
Use FIND_IN_SET:
select * from tbl where FIND_IN_SET(name, '1,2,3,4') = 0
Like the other answer, I would also recommend normalizing your database if at all possible. This query could be slow as it will require a scan of the table. Even if there is an index on name this query won't be able to use it efficiently.
I have a UNIX-type timestamp stored in an INT column in MySQL. What is the proper way to retrieve this as a MySQL DATETIME?
(I found the answer when re-scanning the MySQL Date functions, but didn't see the answer on SO. Figured it should be here.)
FROM_UNIXTIME()
select from_unixtime(column,'%Y-%m-%d') from myTable;
SELECT FROM_UNIXTIME(mycolumn)
FROM mytable
The function STR_TO_DATE(COLUMN, '%input_format') can do it, you only have to specify the input format.
Example : to convert p052011
SELECT STR_TO_DATE('p052011','p%m%Y') FROM your_table;
The result : 2011-05-00
This works for sure.
select from_unixtime(column) from table