mysql rename column output query - mysql

mysql rename column output query
Hey guys!
I'm trying to create a kind of alias to use as a variable in grafana but I'm not getting the desired result...
My intention is that the query returns only the alias created from the query and not the name of the column itself.
See my query below:
select dcontext as Recebidas from cdr_local where dcontext = 'inc_alvoko' group by dcontext;
With this query I get the answer as shown below:
+------------+
| Recebidas |
+------------+
| inc_alvoko |
+------------+
Where is the column name "inc_alvoko" I want to rename it to "Received" but I can't...
I will greatly appreciate any member who can help me with this detail!

seems you are looking for a "translation" for your countents so you could try using case
select distinct
case when dcontext = 'inc_alvoko' then 'Received' END Recebidas
from cdr_local w
where dcontext = 'inc_alvoko'
and if you need distinct result but not use a ggregation function you should not use group by but use DISTINCT clause in select .
the improper use of group bt without aggregation function produce error (by default) in mysql version starting by 5.7

Related

MySQL 5.7 - Error sql_mode=only_full_group_by

After Updating to MySQL 5.7.11 we are getting a number of errors related to the sql_mode including only_full_group_by.
Research shows we can turn this off but it would probably be better to know how to achieve the intended goal with properly structured SQL statements.
Error:
#1140 - In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'tblslideshow.slideImage'; this is incompatible with sql_mode=only_full_group_by
Query:
SELECT
tblslideshow.slideImage,
COUNT(tblslideshow.slideID) AS countVal
FROM
tblslideshow
WHERE
tblslideshow.parentID = 3424
The goal is to return a list of values but also a Count for all returned records.
What's the best way to achieve this goal without having to change the MySQL my.ini ? And why is it a Group By error is we're not even grouping? Or is Grouping part of the proper solution?
Expected Result should be:
slideImage | countVal
Image1.jpg | 3
Image2.jpg | 3
Image3.jpg | 3
Edit: Since we are using PHP and MySQLi it may be wiser to exclude the Count() and just use "mysqli_num_rows"
When you use Aggregate function, you need to add non-aggregate columns on Group by clause.
SELECT
tblslideshow.slideImage,
COUNT(tblslideshow.slideID) AS countVal
FROM
tblslideshow
WHERE
tblslideshow.parentID = 3424
Group by tblslideshow.slideImage
EDIT
You can try to use subquery on select
SELECT t.slideImage,
(
SELECT
COUNT(tblslideshow.slideID) AS countVal
FROM
tblslideshow
WHERE
tblslideshow.parentID = 3424
) countVal
FROM tblslideshow t
WHERE t.parentID = 3424
What you are looking for is COUNT OVER, available as of MySQL 8.0:
SELECT
tblslideshow.slideImage,
COUNT(*) OVER () AS countVal
FROM
tblslideshow
WHERE
tblslideshow.parentID = 3424;
This keeps the single rows and adds the aggregate value to them.

using of NOT LIKE in MYSQL & getting error of SubQuery

I have used below mysql query for fetching data:
select *
from tableName
where tableName.field_type='22'
and tableName.field_id NOT LIKE(select aField_id
from TableName 02 where status !='Active')
I am getting error
1242 - Subquery returns more than 1 row
can you let me know what wrong in this query
select * from tableName
where tableName.field_type='22'
and
tableName.field_id
NOT IN(select aField_id from TableName where status !='Active')
Use not in in place of not like. Not in is for comparison of column with a set of values. not like is for comparison of column with a single value or pattern. Your subquery in returning more than one rows. not like can't handle that.
Like deals with only one input. So you should use IN in place of LIKE.

Show column name MySQL

Is there a way to get a column name from table where all values in this column are the same!
Example! IF i would there would be a such a code it would return answer 'Works'
Table1
ID Name Works
1 Andre Yes
2 John Yes
3 Stewart Yes
I don't know if the columns of the table are known. If not, you could be able to get them by:
desc Table1;
or if you are using higher version of MySQL, you could use:
select column_name from information_schema.columns where table_schema='your_schema' and table_name='Table1';
Then you try the following statement with parameters #column being replaced with the column names retrieved from the above statement:
select count(*) from (select count(*) as c from Table1 as t group by t.#column) as sub;
If the result is 1, the column is what you want. The result means how many distinct values this column has.
I suppose you will have to use a kind of programming language or stored procedure. You are not likely being able to achieve that with one single SQL statement.
I'm not sure if it's possible to run from MySQL itself but you can do this from your scripting engine.
You can run a loop on each column and do a query:
SELECT
COUNT(DISTINCT column_name)
FROM table_name;
And see you get 1 record in the results

How to select table from syntax show on mysql, without using procedure?

i have read this article Select data from “show tables” MySQL query . But i need to get table from show tables. I try this syntax at the first.
show tables from pos where tables_in_pos like (select kdtk from
toko)
and then the value has show
+-------------------+
| tables_in_pos |
+-------------------+
| fnpk |
after that i try this syntax
select * from concat(show tables from pos where tables_in_pos like (select kdtk from toko),'a') as Hasil
How to select table from syntax Show on mysql? without using Procedure.
Use the query as follows,You need to include conditions in your query to get them the way you want:
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='your DB_Name' AND........{additional conditions }....

Sql Unknown column error 'where clause'

I want to run a SQL query (or multiple queries) on database of my website in phpMyadmin in the SQL tab it says
SELECT * FROM `shell` WHERE 1
SO I typed in
SELECT * FROM `shell` WHERE 'http://samiul.ru.cx/c0de.php';
but I get an error
1054 - "Unknown column 'http://samiul.ru.cx/c0de.php' in 'where clause'
The shell should be an existing table on your phpMyadmin.
After the WHERE statement you should place your column name that you want to filter. For example your table shell contain a column name shell_name with a character data type. Your sql statement should look like:
SELECT *
FROM shell
WHERE shell_name like 'any_name';
If your not planning to filter your table you can remove the WHERE statement.
Leaving you only the main sql statement:
SELECT * FROM shell;
The general syntax for a select query with where clause is:
SELECT * FROM TABLE_NAME
WHERE COLUMN_NAME = 'PARTICULAR_COLUMN_VALUE';
Where clause is used to filter records that satisfy a particular filter criteria.
For example, You have a table say EMP_TABLE with following structure and records,
EMP_NAME | EMP_NUMBER
NISHA | 3322
GRASHIA | 3696
If you want to fetch all columns of all records that have name as 'NISHA', then your query should be:
SELECT * FROM EMP_TABLE WHERE EMP_NAME = 'NISHA';
*Note: * in the above query can also be expanded as SELECT EMP_NAME, EMP_NUMBER where you can explicitly mention all the column names
If you want to fetch only particular column say emp number of employee 'NISHA' then your query can be:
SELECT EMP_NUMBER FROM EMP_TABLE WHERE EMP_NAME = 'NISHA';
For more reference on Syntax of WHERE CLAUSE
SELECT * FROM *table* WHERE *column* = *value*
In your case:
SELECT * FROM shell WHERE webpage = 'http://samiul.ru.cx/c0de.php'
Or similar.
The WHERE 1 part is a little confusing.
SELECT * FROM table WHERE 1
is functionally the same as
SELECT * FROM table
Here's an explanation: Importance of WHERE 1 in MySQL queries
I've never actually had a need for it, but maybe someone has.
This is a pretty basic question. You might want to read up a little on how queries work.