I making a SQL statement using union all as the following image:
But the name of the column is the first name used in SQL query. How make this SQL statement without this my error?
Thanks a lot!
Use as to make an alias name. You already do it with the count() column too but did not use as which is optional.
select 'abertos' as some_name, count(fk_id_status) ....
BTW you only need to do it for the first query in the union
Related
SELECT id, JSON_EXTRACT(column,'$.parent[3].child') AS child FROM table
WHERE JSON_EXTRACT(column,'$.parent[3].child') IN (1)
this is my sql query. i have a also column jsonlength how can i connect this 2 columns in one query ? i want to change 3 to dynamic number given in jsonlength
for example:
SELECT id, JSON_EXTRACT(column,'$.parent['jsonlength'].child') AS child FROM table
WHERE JSON_EXTRACT(column,'$.parent['jsonlength'].child') IN (1)
but this query not working.
You need to form the JSON path with string concatenation. In MySQL, this is done with the CONCAT() function.
SELECT id, JSON_EXTRACT(column, CONCAT('$.parent[',jsonlength,'].child')) AS child FROM table
WHERE JSON_EXTRACT(column,CONCAT('$.parent[',jsonlength,'].child')) IN (1)
I try to make some changes to my MySQL database, and I need to change values of this col name "id_lang" which values now is "6" to change to value "2".
This col is found in many tables, it will be great to have a single query which will to this for all DB at once.
by now i found this query,
SELECT REPLACE(yourcolumn,'ValueInTheColumnTobeReplaced', 'NewValue') as replacedColumnName FROM yourtable
PS: I use PHPMYADMIN
but i cant make it work... PLease help me!!!
You have to use an Update query.
But you can not do this in one query for all tables.
Update yourTable set id_lang=2 where id_lang=6
Use this query
UPDATE yourTable SET id_lang=2 WHERE id_lang=6;
Is there any way to do something like :
SELECT * FROM TABLE WHERE COLUMN_NUMBER = 1;
?
No, you can't. Column order doesn't really matter in MySQL. See the below question for more details.
mysql - selecting values from a table given column number
If your table has a column named COLUMN_NUMBER and you want to retrieve rows from the table where that column contains a value of '1', that query should do the trick.
I suspect that what you are trying to do is reference an expression in the select list with an alias. And that is not supported. An expression in the WHERE clause that references a column must reference the column by name.
We can play some tricks with inline views, to give an alias to an expression, but this is not efficient in terms of WHERE predicates, because of the way MySQL materializes a derived table. And, in that case, its a name given to the column in the inline view that has to be referenced in the outer query.
How I did it:
I'm trying to take (last 3 values of) column number 4 in sometable.
set #mydb=(SELECT DATABASE());
set #mycol=(select COLUMN_NAME from information_schema.columns where
table_schema=#mydb and table_name='sometable' and ordinal_position = 4);
SELECT Date,#mycol FROM sometable ORDER BY Date DESC LIMIT 3;
Of course, if Database name is known, first line could by whiped and #mydb replaced by real database name.
You can do this trick
Example:
$query="select * from employee";
$result=mysql_query($query);
$meta=mysql_fetch_field($result,0) // 0 is first field in table , 1 is second one ,,, etc
$theNameofFirstField=$meta->name; // this well return first field name in table
// now you can use it in other query
$seconQuery="select $theNameofFirstField from employee";
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
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.