I am doing a select * on a postgresql table, and everything looks good. But if I do:
SELECT Name from People
It says:
ERROR: column People.Name does not exist
SQL state: 42703
Character: 8
But the name column shows up during select *. I've tried:
SELECT People.Name from People
as well, with the same result. Am I missing something? It should be pretty easy to do this in any other database.
Try putting quotation marks around the column name, i.e. "Name"
PostgreSQL converts everything to lowercase.
If you asks for this:
CREATE TABLE People
{
id SERIAL,
Att1 varchar(100)
-- constraints
};
SELECT Name FROM People;
You should get all the information, because pgsql converts this to
CREATE TABLE people
{
id SERIAL,
att1 varchar(100),
-- constraints
};
SELECT name FROM people;
If you built your table with pgAdmin and created field with mixed casing, you will need to quote them to be sucessfull, like this:
SELECT "Att1" FROM people
Name is a keyword, so it might not be handled well in this case. The best thing to do would be to alias the table like this:
SELECT Name from Peoplep
and then use the p to select the column:
SELECTp.Namefrom People p
Related
I'm working with a database in T-SQL/SQL Server 2016 at the moment which has some stored procedures containing a keyword I'm not familiar with, namely the "DATA" suffix after a query:
SELECT * FROM dbo.TableName DATA
I'm struggling to find any documentation on what the purpose of this "DATA" keyword is. Could someone shed some light please?
It is not some specific keyword. It is just a table alias. Note that if you changed your select to
SELECT DATA.* FROM dbo.TableName DATA
it will work, as the table now has the "DATA" alias. For the same reason, this:
SELECT dbo.TableName.* FROM dbo.TableName DATA
will throw an error.
This is an alias for the table name, usually it is used if we are inner joining the same table more than one time, or when we need to call the table with a shortcut name.
For example if the table has a key named ID, then:
SELECT DATA.* FROM dbo.TableName DATA
where DATA.ID = "1"
is like
SELECT dbo.TableName.* FROM dbo.TableName
where TableName .ID = "1"
I have a table vehicles has a column 'name'
Values are stored like car/tesla, car/honda, truck/daimler (each value is stored as type/brand)
I want to query the table using only brand. If I look up tesla, it should return the row corresponding to car/tesla. How do I do it? I'm using postgres.
There is no need in regex in your case. Just use old good like:
select name
from vehicles
where name like '%/tesla'
2 solutions are available a select query with LIKE operand or a select query with contains operand.
select * from vehicles where name LIKE '%tesla%'
Select * from vehicles where Contains(name, "tesla");
Suppose I have a Table Name
First Name Last Name
--------------------------
Kris Kristos
I want a right syntax for the query which appears like
Select *
from Name
where First_name like '%'Last_Name'%'
You could use strpos:
SELECT *
FROM name
WHERE strpos(LastName, FirstName) > 0;
SqlFiddleDemo
I think I got the answer. 'concat' can be used for this purpose.
Select *
from name
where firstname like concat('%',lastname)
How does wildcards works in sql. If I do select * from table it give all the fields. But if I do select a* from table it gives error. Shouldn't it give all fields which begins with a?
I am little confused.
SELECT * FROM tableName literally means "select all columns from tableName".
Philip Graham is right about his answer where he asked to use a.*
Wildcards help you search for strings about which you are not sure. These are almost always used with the LIKE keyword and put in WHERE clauses or searched CASE statements.
There are two wildcard characters - % and _.
% is used to find any string of 0 or more length.
E.g.,
SELECT firstName
FROM persons
WHERE UPPER(firstName) LIKE 'J%'
This will return all the firstName from the persons table where firstname starts with letter J. This would return "Jason", "James", "Josh", "Jessica" and much more.
Note that UPPER function was used to eliminate case sensitivity.
Next, you can have an _ character that looks for the presence of one single character.
SELECT firstName
FROM persons
WHERE UPPER(firstName) LIKE 'J_M__'
This would return "James", "Jimmy", "Jamos", "Jxmx" and filter away any "Jason", "Jaguar", etc.
For more info click here
You can use a.* where a is the name of the table. For instance in
select a.* from a left join b on a.id = b.id
You would return only the fields from a but not from b
If want to use a wild card in SQL, You need to key on the column that you want to filter using LIKE.
SELECT *
FROM table
WHERE column_name LIKE 'a%';
This will give you everything that begins with 'a' on that column.
If you don't want all the columns, you must explicitly give the name of each column that you want in the query.
SELECT LastName, FirstName, Address
FROM table
So if you want all the fields that begin with 'a' you must name all the fields that begin with 'a' in the SELECT statement.
Hope this helps.
I'm using the Firefox sqlite manager addon and I have a database table named - drinktable. I need to extract all the columns where the title matches a certain string. This is the query I am running right now -
SELECT * FROM drinktable where title = 'First Drink';
But it won't return anything. It just shows the 4 columns in the database, but nothing in those columns. Can someone please tell me why ?
I have a database table named - drinktable.
One thing in your question. Your table name is drinktable and you are querying against first, is it a typo. In SQL Select statement you specify table name after from and column name in where clause
SELECT * FROM drinktable where first = 'First Drink';
Select * from <TableName> where <ColumnName> = 'Value to compare';
If your table name is first and column name is drinktable then chances are that there are now row(s) with column drinktable set to 'First Drink'. You might have spaces or some other characters. Try
SELECT * FROM first where drinktable like '%First Drink%';
EDIT:
Based on your edited question, chances are you have spaces or some other characters, try:
SELECT * FROM drinktable where title like '%First Drink%';