sql columns adding - mysql

I am trying to add two columns as one column by using this statement
SELECT (member_Firstname+''+member_Lastname) AS Name FROM members
but it gives all 0 values in mysql workbench

SELECT concat(member_Firstname,'',member_Lastname) AS Name FROM members;
This should work always

I think that in MySQL you should use CONCAT, as follows:
mysql> SELECT CONCAT('My', 'S', 'QL'); -> 'MySQL'

Adding is for numbers; for joining strings, use concat()
SELECT CONCAT(string1,string2,string3,etc) FROM table

SELECT CONCAT(CAST(int_col AS CHAR), char_col);
CONCAT() returns NULL if any argument is NULL.
So for the example
SELECT CONCAT(member_Firstname, member_Lastname);

Related

How to select a column WHERE its values are in json arrayin MySQL

In a word, I want to
select * from test.population where Number in (1,2,3),
but in the place of (1,2,3) I want to have a function that returns json array. So that I want to have this to be working like this.
select * from test.population where Number in ('[1,2,3]')
How to put json-array into where it clause?
You can use the MEMBER OF operator:
Number member of ('[1,2,3]')
You can use JSON_SEARCH(). It is available since MySQL 5.7, whereas MEMBER OF() came with MySQL 8.0:
select * from test.population where json_search('[1,2,3]', 'one', number) is not null

Running a SQL SELECT statement against a MYSQL column of SET type

I'm trying to run a SQL SELECT statement against a column that is of type SET. The table is called myTable and the columns in myTable are called base_props and names. The base_props column is of type SET. The values in base_prop are vb,nt, cnt,poss and loc. So I would like to SELECT entries from the column 'name' where base_props have both the values, vb and poss. The results I'm looking to get may have values other than just vb and poss. So to be clear I would like to select all entries that have the values vb and poss regardless if they have other values as well. I've tried the following SQL queries but I can't get the desired results.
SELECT name from myTable WHERE base_props = 'vb' AND base_props = 'poss'
That query returns an empty result set. I've tried using FIND_IN_SET() and IN() but I couldn't get anywhere with that. I've written SQL statements before but never had to deal with columns that are type SET. Any help is appreciated.
The only thing I can come up with is using the LIKE keyword:
SELECT name FROM myTable WHERE (base_props LIKE '%vb%' AND base_props LIKE '%poss%');
This will make sure both vb and cnt are in the base_props column. Of course you can use cnt, nt and loc in there, or any number of base_props values in the sql, just add more AND statements.
OR as a deleted answer by samitha pointed out, you can use FIND_IN_SET:
SELECT name from myTable WHERE FIND_IN_SET('vb', base_props) AND FIND_IN_SET('poss', base_props);
Comment (by spencer7593): "both of these work, but there is a slight difference. The LIKE operator will actually match any member that includes the search string anywhere in a term; the FIND_IN_SET function will only match an exact member. It's also possible to search for members in set by the order they appear in the SET definition, using the MySQL BITAND operator: for example, to match the 1st and 4th members of the set: WHERE base_props & 1 AND base_props & 8". So for example, if you have 'a' and 'aaa' in your set, then using the LIKE "%a%" method will also return rows containing 'aaa'.
Conclusion: use the FIND_IN_SET solution since it will work for all cases.
FIND_IN_SET return index, Try this
SELECT name from myTable WHERE FIND_IN_SET(base_props, 'vb') > 0 AND
FIND_IN_SET(base_props, 'poss') > 0

Set default value in select statement(not use UNION statement)

I would like to have a SELECT statement that will return specified default values if no rows are returned from the database.
We can use UNION to get the desired result like this question: "How to set a default row for a query that returns no rows?", but this gives an extra result row.
example:
SELECT a
from TBL_TEST
UNION
SELECT 0
FROM DUAL
Is there a better way, or a standard SQL way to do this that will be portable across multiple database engines?
SELECT ifnull(a,20) FROM TBL_TEST
Selects 20 if a is null otherwise selects a (in mysql, not sure about others)
For a portable solution, how about:
select coalesce(a, 0)
from TBL_TEST
right outer join DUAL on null is null
The COALESCE function is used here because it is more portable than NVL() or IFNULL().
You would have a DUAL table created in database systems that use a different name, such as SQL Server or DB2.
MySQL has the DEFAULT function, but I'm not sure how standard or widely supported it is.
MySQL IFNULL is like oracle's NVL function
MySQL IFNULL() takes two expressions and if the first expression is not NULL, it returns the first expression. Otherwise it returns the second expression.
Syntax
IFNULL(expression1, expression2);
SELECT IFNULL(a,<default value>) from TBL_TEST
In Oracle:
select nvl(a, 0)
from DUAL left join TBL_TEST on null is null
use the COALESCE() to convert the null value column with its default value such as
select coalesce(a,0) from TBL_TEST
In, SQL SERVER 2008 R2 : When Value IS NULL
SELECT ISNULL(a,<Default Value>) from TBL_TEST
e.g. SELECT ISNULL(a,0) from TBL_TEST
In, SQL SERVER 2008 R2 : When Empty String
SELECT ISNULL(NULLIF(a,<Empty String>)<Default Value>) from TBL_TEST
e.g. SELECT ISNULL(NULLIF(a,'')0) from TBL_TEST
This is working fine...

Join a Column with SELECT query in PostgreSQL

Need this equivalent outcome on PostgreSQL on this MySQL query.
select id, 'ID1' from supportContacts
Idea is to Join a Column with table name and row values equals to ID1.
Executing the same query on PostgreSQL gives the desired row values but gives ?column? uknown as column name.
Which PostgreSQL query will give the exact output?
Add a column alias to assign a name of your choosing. Else the system applies defaults.
To assign a type, use an explicit cast. I cast to text here:
SELECT id, 'ID1'::text AS "ID1" FROM supportContacts
Or use the SQL standard cast() to make it portable:
SELECT id, cast('ID1' AS varchar) AS "ID1" FROM "supportContacts"
For portability, make sure that MySQL runs with SET sql_mode = 'ANSI'.
Also, unquoted CaMeL-case names like supportContacts are cast to lower case in PostgreSQL. Use "supportContacts" or supportcontacts depending on the actual table name.
Start by reading the excellent manual here for basics about identifiers.
You can find answer in SQL Fiddle here

Checksum of SELECT results in MySQL

Trying to get a check sum of results of a SELECT statement, tried this
SELECT sum(crc32(column_one))
FROM database.table;
Which worked, but this did not work:
SELECT CONCAT(sum(crc32(column_one)),sum(crc32(column_two)))
FROM database.table;
Open to suggestions, main idea is to get a valid checksum for the SUM of the results of rows and columns from a SELECT statement.
The problem is that CONCAT and SUM are not compatible in this format.
CONCAT is designed to run once per row in your result set on the arguments as defined by that row.
SUM is an aggregate function, designed to run on a full result set.
CRC32 is of the same class of functions as CONCAT.
So, you've got functions nested in a way that just don't play nicely together.
You could try:
SELECT CONCAT(
(SELECT sum(crc32(column_one)) FROM database.table),
(SELECT sum(crc32(column_two)) FROM database.table)
);
or
SELECT sum(crc32(column_one)), sum(crc32(column_two))
FROM database.table;
and concatenate them with your client language.
SELECT SUM(CRC32(CONCAT(column_one, column_two)))
FROM database.table;
or
SELECT SUM(CRC32(column_one) + CRC32(column_two))
FROM database.table;