i want result -> Student_id,Name,Address,Marks
Thank you.
If you have several other columns and want to display some of them, then use :
SELECT Student_id, Name, Address, Marks
FROM Student
Otherwise, if you only have these 4 columns in your table, then :
SELECT *
FROM Student
If you only want the names of your columns without data, then :
SELECT * FROM Student WHERE 1=0
/*or*/
SELECT Student_id, Name, Address, Marks FROM Student WHERE 1=0
MYSQL, MS SQL and Postgresql (thanks #christophe)
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_SCHEMA = 'database_name'
AND TABLE_NAME = 'table_name'
PIVOT the results if you need column names in one line
Something like this should get you the column names, if that's what you're looking for.
select *
from table
where 1=2
<%
Dim rs1, i
Set rs1 = Server.CreateObject( "ADODB.Recordset" )
set rs1 = yourconnection.execute( "SELECT * FROM Student" )
For i=0 to rs1.Fields.Count-1
Response.Write rs1.fields(i).name & ", "
Next
%>
you will get: Student_id, Name, Address, Marks
Related
With this query:
SELECT *
FROM table1
WHERE name = 'Peter'
I can retrieve all data from Peter from table1. This can be done with the "Wildcard *".
Question
Is there any kind of wildcard for the WHERE part? For example:
SELECT *
FROM table1
WHERE name = *
This option of course not working, but I am looking for a wild card there so that all names will be included in my query. I know it's easier to remove the WHERE statement, but due to some reasons I still need it.
SELECT *
FROM table1
WHERE True OR name = 'Peter'
;
This may look silly, but it can come in handy when generating query strings, eg in PHP/PDO:
$query = "SELECT * FROM names
WHERE ($ignore_name OR name = :the_name)
AND ($ignore_address OR address LIKE :the_address)";
, where the $ignore_xxx variables are either True or False, and completely under your control (not user-input!)
select *
from table1
where name = 'Peter' or name = name;
You can query output into your WHERE clause like so:
SELECT *
FROM table1
WHERE [name] IN (SELECT DISTINCT [name]
FROM table1)
I am implementing a tool to clean up all customer names across various tables in a schema called stage. The customer names could be coming from columns billing_acc_name or cust_acc_names. I do not know in advance how many tables have these columns, but as long as they do, they will be part of clean up.
However, prior to clean up, I need to select all unique customer names across the tables in the schema.
For better separation of concerns, I am looking at implementing this in PL/pgSQL. Currently, this is how I'm implementing this in Python/pandas/SQLAlchemy etc.
table_name = 'information_schema.columns'
table_schema_src = 'stage'
cols = ['billing_acc_name', 'cust_acc_name']
# get list of all table names and column names to query in stage schema
sql = text(f"""
SELECT table_name, column_name FROM {table_name} WHERE table_schema ='{table_schema_src}'
AND column_name = ANY(ARRAY{cols})
""")
src = pd.read_sql(sql, con=engine)
# explore implementation in pgsql
# establish query string
cnames = []
for i, row in src.iterrows():
s = text(f"""
SELECT DISTINCT upper({row['column_name']}) AS cname FROM stage.{row['table_name']}
""")
cnames.append(str(s).strip())
sql = ' UNION '.join(cnames)
df = pd.read_sql(sql, con=engine)
The auto-generated SQL query string are then as below:
SELECT DISTINCT upper(cust_acc_name) AS cname FROM stage.journal_2017_companyA UNION
SELECT DISTINCT upper(billing_acc_name) AS cname FROM stage.journal_2017_companyA UNION
SELECT DISTINCT upper(cust_acc_name) AS cname FROM stage.journal_2017_companyB UNION
SELECT DISTINCT upper(billing_acc_name) AS cname FROM stage.journal_2017_companyB UNION
SELECT DISTINCT upper(cust_acc_name) AS cname FROM stage.journal_2017_companyC UNION
SELECT DISTINCT upper(billing_acc_name) AS cname FROM stage.journal_2017_companyC UNION
SELECT DISTINCT upper(cust_acc_name) AS cname FROM stage.journal_2017_companyD UNION
SELECT DISTINCT upper(billing_acc_name) AS cname FROM stage.journal_2017_companyD
The plpgsql function may look like this:
create or replace function select_acc_names(_schema text)
returns setof text language plpgsql as $$
declare
rec record;
begin
for rec in
select table_name, column_name
from information_schema.columns
where table_schema = _schema
and column_name = any(array['cust_acc_name', 'billing_acc_name'])
loop
return query
execute format ($fmt$
select upper(%I) as cname
from %I.%I
$fmt$, rec.column_name, _schema, rec.table_name);
end loop;
end $$;
Use:
select *
from select_acc_names('stage');
Hi I am writing oracle query to support select all for in clause my query goes something like this
SELECT * FROM country
WHERE
country_id in( IF('test' = 'test',(1,2,3),true) )
If condition ('test' = 'test') is true then it should fire query like
SELECT * FROM country WHERE country_id in(1,2,3)
Else it should fire query
SELECT * FROM country WHERE country_id in(true)
If I understand correctly what you need you will have to split it into 2 conditions:
SELECT *
FROM country
WHERE(('test'='test')AND(country_id IN (1,2,3)))
OR(('test'<>'test')AND(country_id<>0))
In SQL Server I had the very convenient ability to make a query like this:
SELECT phone_number, last_known_location, *
FROM missing_female_pilots
WHERE last_name = 'Earhart'
How can I do something similar in Oracle?
You can use table alias :
SELECT t.phone_number, t.last_known_location, t.*
FROM missing_female_pilots t WHERE t.last_name = 'Earhart'
Or just prepend table name before * :
SELECT phone_number, last_known_location, missing_female_pilots.*
FROM missing_female_pilots WHERE last_name = 'Earhart'
I think that is not easy to understand what I need reading title, so I'll explain better.
My database is like:
Table1Table2Table3 and other tables like these...
Users will insert into textfield a value like 1 and I will show them only the name that contains 1. I do this in this way:
ArrayList<String> found = new ArrayList<String>();
ArrayList<String> allTable = getAllTableInDB();
for(String table:allTable){
try {
ResultSet rs = stat.executeQuery("SELECT * FROM '"+table+"'");
while(rs.next()){
if(!found.contains(rs.getString("name")) && rs.getString("name").equals(WhatUserInserts))
found.add(rs.getString("name"));
}
} catch (SQLException ex) {
ex.printStackTrace();
Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
}
}
What I want to do now is to calculate the "best" of this name found ordering by average of column v.
For one table I know that I can use something like:
SELECT name FROM table GROUP BY name ORDER BY AVG(v) DESC
How can I do the same on this limit of value of some table?
I hope I explain my question well
SOLVED in this way THANKS TO Dan P:
ArrayList<String> tables = getAllTableInDB();
String query = "SELECT * FROM (";
String union = " UNION ";
for(int i=0;i<tables.size();i++){
query+="SELECT * FROM '"+tables.get(i)+"' WHERE name LIKE '%"+find+"%'";
query = i==tables.size()-1?query:query+union;
}
query+=") GROUP BY nome ORDER BY AVG(venduti) DESC";
Assuming you want the Max or Avg of all tables this will get you the max V value for name. If you want Avgerage use AVG. I didn't exactly understand the question but I think this what you want.
SELECT Name, Max(V)
FROM
(
SELECT Name, v
FROM TABLE1
UNION
SELECT Name, v
FROM TABLE2
UNION
SELECT Name, v
FROM TABLE3
) AS AllTables
GROUP BY Name
WHERE Name = 'Jon Doe'
After commenting you asked if you could enhance performance. Yes by including the name in the where clause for each table assuming there is an index / primary key on the name. You should probably used a table valued function if you go this route.
DECLARE #Name NVARCHAR(50)
SET #Name = 'Jon Doe'
SELECT Name, Max(V)
FROM
(
SELECT Name, v
FROM TABLE1
WHERE Name = #Name
UNION
SELECT Name, v
FROM TABLE2
WHERE Name = #Name
UNION
SELECT Name, v
FROM TABLE3
WHERE Name = #Name
) AS AllTables
GROUP BY Name