I have an sql syntax in my php script that gives me an error. All I want is to get the result of 2 sql statemet.
$sql1 = " SELECT `name`, `surname`, `email`, `user_id` FROM users WHERE (surname LIKE '$name%' AND name LIKE '$surname%') OR (surname LIKE '$surname%' AND name LIKE '$name%') ";
and
$sql2 = " SELECT profile_id from profile WHERE `country`='uk' ";
I want to get values from my database for both $sql1 and $sql2. I use UNION statement like this but it did not work. Any ideas how to fix it :
$sql = " (SELECT `name`, `surname`, `email`, `user_id` FROM users WHERE (surname LIKE '$name%' AND name LIKE '$surname%') OR (surname LIKE '$surname%' AND name LIKE '$name%')) UNION (SELECT profile_id from profile WHERE `country`='uk' ) ";
A UNION query must have same output fields.
The select queries that you combine in a union query must have the
same number of output fields, in the same order, and with the same or
compatible data types.
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 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
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))
SELECT a.VPNID||'|'||a.PUBLICNUMBER||'|'||a.PRIVATENUMBER from virtualterminal a
UNION ALL
SELECT b.VPNID||'|'||b.PUBLICNUMBER||'|'||b.PRIVATENUMBER||'|'||b.PROFILEID from terminal b;
This is the code , but in the first statement as you can see there is no PROFILEID column. I would like to write " null " to that column because we don't know the PROFILEID of them. How can I write "null" to that column ?
Try this Query:
SELECT a.VPNID||'|'||a.PUBLICNUMBER||'|'||a.PRIVATENUMBER||'|'||'NULL'
from virtualterminal a
UNION ALL
SELECT b.VPNID||'|'||b.PUBLICNUMBER||'|'||b.PRIVATENUMBER||'|'||b.PROFILEID
from terminal b;
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'