Concat a string to SELECT * MySql - mysql

The following query works fine with MySQL:
SELECT concat(title,'/') FROM `socials` WHERE 1
It Concat / to the selected title field.
However, when I try to do the following:
SELECT concat(*,'/') FROM `socials` WHERE 1
It returns the follwoing Error:
#1064 - You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to use near '*,'/') FROM `socials` WHERE 1 LIMIT 0, 30' at line 1
So is there any way to make such sql query to work with MySql

You simply can't do that in SQL. You have to explicitly list the fields and concat each one:
SELECT CONCAT(field1, '/'), CONCAT(field2, '/'), ... FROM `socials` WHERE 1
If you are using an app, you can use SQL to read the column names, and then use your app to construct a query like above. See this stackoverflow question to find the column names: Get table column names in mysql?

If you want to concatenate the fields using / as a separator, you can use concat_ws:
select concat_ws('/', col1, col2, col3) from mytable
You cannot escape listing the columns in the query though. The *-syntax works only in "select * from". You can list the columns and construct the query dynamically though.

You cannot concatenate multiple fields with a string. You need to select a field instand of all (*).

You cannot do this on multiple fields. You can also look for this.

Related

Simplest way to do an array literal in MySQL

I would like to get the array [1,2,3] in mysql. For example:
with tbl (id) as (
select 1 union select 2 union select 3
) select 'something', json_arrayagg(id) from tbl
# something, json_arrayagg(id)
# 'something', '[1, 2, 3]'
Would there be a simpler way to do this rather than sort of 'building up a table' in a CTE and then grouping it outside it?
For example, I am trying to do something like this (if possible):
SELECT '1', [1,2,3]
MySQL '8.0.20' Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[1,2,3]) ids from sort group by letter order by ids asc SELECT '1', [1,2,3]' at line 4
After much commenting above, your actual question has come out, which is that you are confused about how to work with JSON literals in MySQL. Valid JSON literals in MySQL always are surrounded by single quotes, just like string literals. Consider this small example:
WITH yourTable AS (
SELECT '1' AS id, '[1,2,3]' AS array
)
SELECT Id, JSON_EXTRACT(array, '$[2]') AS third
FROM yourTable;
This outputs 1, 3 as the demo link shows. The key here is that the JSON array literal, which you already know how to form, is in single quotes.

MySQL: Ordering of columns when using a wildcard with group by has odd behavior

What is the difference between these two MySQL statements?
Works:
select *, count(mycol) c from mytable group by mycol;
Doesn't work:
select count(mycol) c, * from mytable group by mycol;
The first statement works as I'd expect, while the second one gives me a syntax error. Why does the order matter?
I'm having trouble finding an answer from Google, because I'm not entirely sure if I'm asking the question correctly.
Edit:
Here's the sanitized error message. I'm using MySQL Workbench, if that's relevant.
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* from mytable group by id' at line 1
Just alias the table and the syntax error will go away.
select count(t.id) c, t.* from mytable t group by id;
See this db fiddle.
It looks like MySQL allows bare (unqualified) * only as immediatly following SELECT. The following query also raises a syntax error :
select 1, * from mytable t;
The documentation prevents against using bare * combined with other items in the SELECT list :
A select list consisting only of a single unqualified * can be used as shorthand to select all columns from all tables.
Use of an unqualified * with other items in the select list may produce a parse error. To avoid this problem, use a qualified tbl_name.* reference.

MySQL - Using * in a conditional statement

I am attempting to execute the following statement...
SELECT
SUM(CASE WHEN CLG ='A*' THEN 1 END) as A*
From Grades
However, I receive the following error...
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*,"
I realise of course that * is used in select statements to select all the rows of a table. However in my case, I want to find the number of records that contain the value of A*. Would anyone be able to point out what I can do to solve this error without having to remove the A* values from my table?
You need to use backticks:
SELECT SUM(CASE WHEN CLG ='A*' THEN 1 END) as `A*`
From Grades;
Actually you could skip CASE:
SELECT SUM(CLG ='A*') as `A*`
From Grades
Your problem is the column alias. Don't use inappropriate characters for column names. Do something like this:
SELECT SUM( CLG = 'A*' ) as A_star
From Grades;
Having to deal with identifiers that use unusual characters is just a pain -- making queries hard to write and to read.

SQL Like with double pipe concatenation

I'm having a problem with a SQL query that must match the username of a user out of a column that contains all the users usernames.
So the column will contain something like:
|USER1|USER2|USER3|USER11|USER22|
The user have pipes on the left and right to prevent "USER1" be matched even in "USER11".
My query is
SELECT * FROM myTable WHERE CONCATUSERS LIKE ('%|' || 'USER1' || '|%')
Note that the USER1 in the query is a variable generated from our code so I must keep the concatenation syntax and I must use a standard syntax too (the code will run in mySQL, SQLServer etc..
So what is the correct way of concatenating strings in a LIKE clause?
MySQL uses the double pipes for concat. SQL Server you can use +.
SELECT *
FROM myTable
WHERE CONCATUSERS LIKE ('%' + '|user1|' + '%')
Use CONCAT.
(available on SQL server 2012 and beyond)
It has the benefit that it implicitly converts types to add the value to the string. And it's not just available on Sql Server and MySql.
SELECT * FROM myTable WHERE CONCATUSERS LIKE CONCAT('%|','USER1','|%');
Do note that in MySQL the result will be NULL if one of the concatenated values is NULL. But not on Sql Server.
It's just Oracle that's being stubborn by only allowing 2 values to that function.
So if the SQL needs to run unchanged on MySQL, a recent Sql Server AND Oracle then this should work:
SELECT * FROM myTable WHERE CONCATUSERS LIKE CONCAT(CONCAT('%|','USER1'),'|%');
You can go with CONCAT function.
As it is supported in both SQL and MySQL
SELECT *
FROM xpat
WHERE plname LIKE concat('%' ,'|user1|' ,'%');

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