I am using mysql workbench (SQL Editor). I need copy the list of columns in each query as was existed in Mysql Query Browser.
For example
Select * From tb
I want have the list of fields like as:
id,title,keyno,......
You mean you want to be able to get one or more columns for a specified table?
1st way
Do SHOW COLUMNS FROM your_table_name and from there on depending on what you want have some basic filtering added by specifying you want only columns that data type is int, default value is null etc e.g. SHOW COLUMNS FROM your_table_name WHERE type='mediumint(8)' ANDnull='yes'
2nd way
This way is a bit more flexible and powerful as you can combine many tables and other properties kept in MySQL's INFORMATION_SCHEMA internal db that has records of all db columns, tables etc. Using the query below as it is and setting TABLE_NAME to the table you want to find the columns for
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='your_table_name';
To limit the number of matched columns down to a specific database add AND TABLE_SCHEMA='your_db_name' at the end of the query
Also, to have the column names appear not in multiple rows but in a single row as a comma separated list you can use GROUP_CONCAT(COLUMN_NAME,',') instead of only COLUMN_NAME
To select all columns in select statement, please go to SCHEMAS menu and right click ok table which you want to select column names, then select "Copy to Clipboard > Select All statement".
The solution accepted is fine, but it is limited to field names in tables. To handle arbitrary queries would be to standardize your select clause to be able to use regex to strip out only the column aliases. I format my select clause as "1 row per element" so
Select 1 + 1 as Col1, 1 + 2 Col2 From Table
becomes
Select 1 + 1 as Col1
, 1 + 2 Col2
From Table
Then I use simple regex on the "1 row per select element" version to replace "^.* " (excluding quotes) with nothing. The regex finds everything before the final space in the line, so it assumes your column aliases doesn't contain spaces (so replace spaces with underscore). Or if you don't like "1 row per element" then always use "as" keyword to give you a handle that regex can grasp.
Related
I use PostgreSQL 13 on windows 10. I have a table with text-type columns. I'm looking for a query to find all the rows that contain just a single word is not accurate in a specific column.
Any ideas?
My table in database the form of, I try to extract all the rows with a single word.
output:
alvi
alexander
elmar
mahmoud
you can try this :
SELECT Name
FROM your_table
WHERE Name ~ '^\w+$'
see the power of the regular expressions in the manual
I'm trying to write a query in sql to exclude a keyword:
It's a list of cities written out (e.g. AnnArbor-MI). In the list there are duplicates because some have the word 'badsetup' after the city and these need to be discarded. How would I write something to exclude any city with 'badsetup' after it?
Your question title and content appear to be asking for two different things ...
Query cities while excluding the trailing 'badsetup':
SELECT regexp_matches(citycolumn, '(.*)badsetup')
FROM mytable;
Query cities that don't have the trailing 'badsetup':
SELECT citycolumn
FROM mytable
WHERE citycolumn NOT LIKE '%badsetup';
In psql, to select rows excluding those with the word 'badsetup' you can use the following:
SELECT * FROM table_name WHERE column NOT LIKE '%badsetup%';
In this case the '%' indicates that there can be any characters of any length in this space. So this query will find any instance of the phrase 'badsetup' in your column, regardless of the characters before or after it.
You can find more information in section 9.7.1 here: https://www.postgresql.org/docs/8.3/static/functions-matching.html
i have a database table with repetitive values in every cell and also same values separated by commas in particular cell, if i need to count all the occurrences of particular name (ex : Facebook) what query i must use(amazon red shift)
A typical SQL query would be:
select count(1) from table t1 where t1.column_name = 'Facebook';
This is if the data cells contain just that word exactly.
Or you might have:
select count(1) from table t1 where t1.column_name like '%Facebook%';
This is if you want to count rows containing word 'Facebook' (among other strings).
I have a table that has a PKey, a FKey, a LineNum, and a TextLine.
In my table, I have multiple results from the FKey. It's a 1 to many relationship.
What I want to do is have the TextLines that match the FKey be concatenated into a single row. (The reason for this is that we're converting from an old COBOL database to T-SQL, and transferring the information to a new database with a different structure, where these "Comments" will all be handled by a single field)
My end query will look something like this:
SELECT Fkey, Line1 + Line2...,
FROM Table1
The issue is that there is a non-consistent number of lines. In addition, I'm trying to avoid any dynamic queries, because I want un-trained/basic users to be able to modify and customize this query. Is there any way to do this?
You could do something like this to get all the data in a single row:
select
t.FKey,
STUFF((SELECT ',' + textline
from Table1 where FKey = t.FKey
FOR XML PATH('')), 1, 1, '') as ConcatTextLines
from
Table1 t
group by t.FKey
There will be some size limitations on the ConcatTextLines column so this may not be applicable if you have thousands of line for some foreign keys.
I want to remove duplicate rows return from a SELECT Query in Postgres
I have the following query
SELECT DISTINCT name FROM names ORDER BY name
But this somehow does not eliminate duplicate rows?
PostgreSQL is case sensitive, this might be a problem here
DISTINCT ON can be used for case-insensitive search (tested on 7.4)
SELECT DISTINCT ON (upper(name)) name FROM names ORDER BY upper(name);
Maybe something with same-looking-but-different characters (like LATIN 'a'/CYRILLIC 'а')
Don't forget to add a trim() on that too. Or else 'Record' and 'Record ' will be treated as separate entities. That ended up hurting me at first, I had to update my query to:
SELECT DISTINCT ON (upper(trim(name))) name FROM names ORDER BY upper(trim(name));
In Postgres 9.2 and greater you can now cast the column to a CITEXT type or even make the column that so you don't have to cast on select.
SELECT DISTINCT name::citext FROM names ORDER BY name::citext