I have a Postgres table that has names and addresses. Some of these name fields are both names of a couple -- for example, "John & Jane".
I am trying to write a query that pulls out only those rows where this is the case.
When I run this query, it selects 0 rows even though I know that they exist in the table:
SELECT count(*) FROM name_list where namefirst LIKE '%&%';
Does anyone know how to address this?
Related
For data profiling purpose , I just need to get the idea if a columns in a given table has values populated or not. For that, I need to get the list of columns and distinct value counts for a given db2 table.
If you are using Db2 for Linux, Unix or Windows you could try
SELECT TABSCHEMA, TABNAME, COLNAME, COLCARD FROM SYSCAT.COLUMNS
I am joining two tables house and tower, both have some of the same column names such as id, created_at, deleted, address etc. I wonder if it is possible to return the columns in the following fashion: house.created_at, house.id, tower.created_at, tower.id etc. I know I can query with AS, I was wondering if it is possible to query something like this: SELECT house.* AS house, tower.* AS tower. I tried it like this, but it was not valid SQL. Any idea how I can chase the column names prefix easily ?
What I want to do;
I have two tables with two address columns , both stored as text I want to create a view returning the matching rows.
What I've tried;
I've created and index on both columns and tables as below;
CREATE INDEX idx_table1_fulladdress ON table1 (LOWER(fulladdress_ppd));
Then run the following;
CREATE OR REPLACE VIEW view_adresscheck AS
SELECT
--from table1
table1.postcode,
table1.fulladdress_ppd,
--from table2
table2.epc_postcode,
table2.fulladdress_epc
FROM
table1,
table2
WHERE
table1.postcode = table2.epc_postcode
AND
table2.fulladdress_epc = table1.fulladdress_ppd ::text;
What hasn't worked
The above returned fewer records than I know to be there. On inspection this is because the address format is not consistent between the two tables ie.
table1.fulladdress_ppd = Flat 2d The building the street
table2.fulladdress_epc = Flat 2/d The building the street, the town
The address isn't consistently formatted within the table either ie in table not all addresses include town so I can't use regex or trim to bulk clean.
I've then seen the fuzzystrmatch module in postgres and this sounds like it might resolve my problem.
Question
Which of Soundex, Levenshtein, Metaphone is most appropriate. Most records are in English by some place names are Gaelic running on 9.6.
talking from experience of matching address from different sources. What you could do is index each address. Regardless of formatting the above address would return the same number. You then match on these indexes.
eg in the UK you have what are called UDPRN numbers for each postal address in the country.
The use case is, say you have a table with a lot of columns (100+) and you want to see if a certain column name exists in the table. Another use case is, say there is a name scheme for the columns in the table that allows me to search for a term that will show all fields with that name - e.g. all fields related to a payment card are prefixed with "card_".
In MySQL I could handle both cases above by doing a show fields in <table_name> like '%<search_term>%'. I've googled for a solution but have only found results related to filtering actual table names and showing table schemas (e.g. \d+), which is not what I am looking for. I've also tried variations of the MySQL command in the psql shell, but no luck.
I'm looking for a way to do this with SQL or with some other Postgres built-in way. Right now I'm resorting to copying the table schema to a text file and searching through it that way.
You can query information_schema.columns using table_name and column_name. For example:
>= select table_name, column_name
from information_schema.columns
where table_name = 'users'
and column_name like '%password%';
table_name | column_name
------------+------------------------
users | encrypted_password
users | reset_password_token
users | reset_password_sent_at
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.