i want to give space between words in alias name like first_name has to be
'First Name'.
i tried this query (SELECT first_name AS 'First Name' FROM employee) but it
showing query parsing error.
you can try SELECT first_name AS (backtick)First\nName(backtick) FROM employee
Related
I have a database table with name 'student' and have column 'name'.
I want to retrieve the names of students starting without a title 'miss' but starting with 'miss'.
I did:
select name from students where lower(name) like 'miss%' and name not like 'miss %'
The above query returns names with title also.
Any help will be highly appreciated.
do you maybe just need lower() around "name" the second time? For me with Postgres 9.5 your query works, if you say "and lower(name) not like 'miss %'
I am creating a search tool for Postgres. It will have several text entries (one for each table column). When the text entry is blank, I want the query to match each and every table entry. Is this possible? I have tried:
SELECT name FROM contacts WHERE surname = '*';
SELECT name FROM contacts WHERE surname = *;
As you can imagine, this returns nothing since there is no surname of *, and the second query is invalid. Any ideas?
EDIT:
Because of the nature of what I am doing, a SELECT name FROM contacts is not sufficient. I guess I could make it work, but it would be ugly. I want a WHERE for every column in the table, but if the search for any given column is an empty string, I want it to fetch every entry in the table.
OK, so I found my answer here. The correct code is:
SELECT name FROM contacts WHERE surname ~* '';
returns all entries
SELECT name FROM contacts WHERE surname ~* 'John Doe';
returns entries that have a surname of John Doe.
I want the query to match each and every table entry
Just use SELECT without WHERE surname clause. Simple as that.
SELECT name FROM contacts
But if you want to use one query, you can do it this way:
cur.execute(
"""SELECT name FROM contacts
WHERE (CASE WHEN %(entry)s != '' THEN surname = %(entry)s ELSE true END)"""
, {'entry': "John Doe"}
)
As for your answer, you used regular expressions matching. In the question you never mentioned it. So I assume it's not what you wanted. Regular expressions have specific syntax, and if some invalid regex format is entered into the text entry, the query will fail.
I would like to get row from Postgres database.
This row in db have to parameters(column): firstname and lastname.
Regular Select query would download theme as separate fields.
but I would like to concatenate them with dot between them.
I would like to get from row:
firstname lastname
one value as follow:
firstname.lastname
Currently I do something like this:
SELECT (firstname, lastname) AS user FROM users
But In result I am getting:
(firstname,lastname)
Can You help?
Use the following
SELECT (firstname::text || '.'::text || lastname::text) AS user FROM users
SELECT user_id as user_id, CONCAT(first_name,' ',last_name) as name
FROM users u
WHERE (first_name like '%r%' or last_name like '%r%')
UNION
SELECT provider_id as provider_id, provider_name as name
FROM providers
WHERE ( provider_name like '%r%')
Using the above query i get
user_id name
5 Richard
6 Rowen
12 Riley
21 Rowen providers
Rowen providers has a provider_id which is 21. but the column name is user_id. How can i get a different column name for provider_id ?
You can split the column in two and supply fake values where you can't get an actual value:
SELECT '' as provider_id, user_id as user_id, CONCAT(first_name,' ',last_name) as name
FROM users u
WHERE (first_name like '%r%' or last_name like '%r%')
UNION
SELECT provider_id as provider_id, '' as user_id, provider_name as name
FROM providers
WHERE ( provider_name like '%r%')
Since you're doing a union, the second query is inheriting column names from the first query. This is causing your data to be inaccurate after execution. If I were you, I'd do a JOIN instead of a UNION. This will preserve column names of the second query, and will present the data in a manner that is easily readable. For rows that are providers, the user information will be NULL, and for rows that are users, the provider information will be NULL. The best suggestion I can give you is to try not to combine two different types of data in one column whenever possible. User_Id and Provider_Id should not be in the same column since they have different meanings.
Try:
SELECT u.user_id, CONCAT(u.first_name,' ',u.last_name) as user_name,
p.provider_id, p.provider_name
FROM users u, providers p
WHERE (u.first_name like '%r%' or u.last_name like '%r%') or
(p.provider_name like '%r%')
I have a SQL query, like so:
SELECT DISTINCT ID, Name FROM Table
This brings up all the distinct IDs (1...13), but in the 13 IDs, it repeats the name (as it comes up twice). The order of the query (ID, Name) has to be kept the same as the app using this query is coded with this assumption.
Is there a way to ensure there are no duplicates?
Thanks
You can try :
select id, name from table group by id,name
But it seems like distinct should work. Perhaps there are trailing spaces at the end of your name fields?
Instead of using DISTINCT, use GROUP BY
SELECT ID, Name FROM Table GROUP BY ID, Name