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

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 }....

Related

mysql rename column output query

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

How do i get all table names who match a specific regex in mysql 8.0?

I'm trying to retrieve all tables with specific name format,
for performing union among those tables.
I'm using mysql Ver 8.0.13, and i wrote this following query for retrieving the relevant tables:
show tables LIKE REGEX '^table_.+_class$';
I couldn't figure out the correct syntax for this query :/
Afterwards i'm planning to union all those tables.
I would like to avoid writing this code since it doesn't scale nicely:
SELECT * FROM table_french_class
UNION
SELECT * FROM table_history_class
UNION
SELECT * FROM table_pingpong_class
UNION
SELECT * FROM table_math_class
UNION
SELECT * FROM table_literature_class
Can someone suggest me how to handle this issue?
Thank you
You could use INFORMATION_SCHEMA catalog:
SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME REGEXP '^table_.+_class$';

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

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.

How to effectively search through MySQL DB with multiple searcheable columns?

I have a table with 60 boolean (TINYINT(1)) searcheable columns. User has possibility of using any subset of the given columns as search condition. Based on that I cannot create a good index for my needs. I was wondering if I can create another column (concat_col) of type BIT(60) that would be concatenation of the searchable columns, i.e.
Table_A:
id |col1|col2|...|col60|concat_col
9999 | 1 | 0 |...| 1 |10...1
I could then create a good index for it (on concat_col) but there is one problem - how do I create a query for it?
Please see this example written in pseudo code:
Standard version (This would obviously work fine):
SQL = SELECT * FROM Table_A WHERE col1=1 AND col60=1
My version ('*' is wildcard because it is not '1' neither '0'):
SQL = SELECT * FROM Table_a WHERE concat_col = '1*...1'
Is there any possibility of solving this problem effectively? Thank you very much for your help!
try with:
SQL = SELECT * FROM Table_a WHERE concat_col REGEXP '^1[0-9a-zA-Z]{58}1$'