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
Related
Is there a way to combine two finds in MongoDB similar to the SQL subqueries?
What would be the equivalent of something like:
SELECT * FROM TABLE1 WHERE name = (SELECT name FROM TABLE2 WHERE surname = 'Smith');
I need to get an uuid from one collection searching by email and then use it to filter in another. I would like if possible to make it with just one find instead of get the uuid, store it in a variable and then search second time using the variable...
Here are the two that I want combined somehow:
db.getCollection('person').find({email:'perry.goodwin#yahoo.com'}).sort({_id:-1});
db.getCollection('case').find({applicantUuid:'4a17e96c-caf9-4d78-a853-73e190005c63'});
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.
there is a problem in mongo query i want like this:
suppose table is:
Table name: Employee
Fields: id, name, salary,age
select * from employee where name="xyz" group by salary;
if i use this query in mongodb so i have to write like this:
db.employee.aggregate([{$match:{"name":"xyz"}},{$group:{"_id":"$salary"}}]);
but i am not getting all the fields only getting salary we can do this if we specify fields name using $first but i dont wanna specify the fields name i need something like select * functionality in mongodb aggregation query.
I have the following postgres query:
SELECT SUM(Cost)
FROM DB
WHERE ID NOT IN (<parameter>)
<parameter> is a dynamic text field where multiple ID's need to be inserted. If you type in
123, 456
as ID's, it results in:
SELECT SUM(Cost)
FROM DB
WHERE ID NOT IN ('123,456')
Which doesn't run properly.
I can change the query, but I can't change the input field. If you type in
123','456
It results in:
SELECT SUM(Cost)
FROM DB
WHERE ID NOT IN ('123'',''456')
When you change the query into:
SELECT SUM(Cost)
FROM DB
WHERE ID NOT IN ('<parameter>')
And you type in
123,456
Then it results in:
SELECT SUM(Cost)
FROM DB
WHERE ID NOT IN (''123'',''456'')
I've got it working for Mysql, but not for Postgresql. Any idea how to trick postgresql?
Try something like:
SELECT SUM(Cost)
FROM DB
WHERE ID != ALL(('{'||'123,456'||'}')::numeric[])
It will form array string from your input values : {123,456}, cast it to an array and check ID against all elements of array.
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