If I've got a table containing Field1 and Field2 can I generate a new field in the select statement? For example, a normal query would be:
SELECT Field1, Field2 FROM Table
And I want to also create Field3 and have that returned in the resultset... something along the lines of this would be ideal:
SELECT Field1, Field2, Field3 = 'Value' FROM Table
Is this possible at all?
SELECT Field1, Field2, 'Value' Field3 FROM Table
or for clarity
SELECT Field1, Field2, 'Value' AS Field3 FROM Table
Yes - it's very possible, in fact you almost had it!
Try:
SELECT Field1, Field2, 'Value' AS `Field3` FROM Table
Related
Sample data:
I want to get row 1, 5, and 10. I hope you could help me, Thanks.
If you want to have distinct data based on different columnn data, you should use GROUP BY query.
If you have table called tbl which has column field1, field2 and field3 and you want to get all rows which has distinct data for all field in 'tbl', then your query should be like this :
SELECT field1, field2, field3
FROM tbl
GROUP BY field1, field2, field3
I have a PostgreSQL database, with only SELECT permissions. In this DB there are two tables with the same structure (the same columns).
I need to write several query in each table and join the results.
There is a way for writing a query like this one?
SELECT
field1,
field2,
field3
FROM
table1
AND
table2
WHERE
condition;
Select from 2 tables. Query = table1 OR table1 + table2 have no answer and it is not my question.
UNION ALL
SELECT field1, field2, field3
FROM table1
WHERE condition
UNION ALL
SELECT field1, field2, field3
FROM table2
WHERE condition;
Or to simplify your WHERE condition
SELECT * FROM
( SELECT field1, field2, field3
FROM table1
UNION ALL
SELECT field1, field2, field3
FROM table2
)
WHERE condition;
You can use Union:
SELECT
field1,
field2,
field3
FROM
table1
UNION
SELECT
field1,
field2,
field3
FROM
table2
SELECT * FROM
( SELECT field1, field2, field3
FROM table1
UNION ALL
SELECT field1, field2, field3
FROM table2
)
WHERE condition;
I would like to insert data from specific fields from one table in another table + some static values. Roughly I would like to do this:
INSERT INTO TableA(Field1, Field2, Field3)
SELECT Field1, Field2, 'staticvalue', Field3
FROM TableB
WHERE TableB.Field6 = 'XYZ'
Any idea how can I mix both specific fields from one table and static values?
You're on the right track... you need to supply a column for the static value to be inserted into.
INSERT INTO TableA(Field1, Field2, someOtherField, Field3)
SELECT Field1, Field2, 'staticvalue', Field3
FROM TableB
WHERE TableB.Field6 = 'XYZ'
I have a select statement like this
select field1, field2
from table1
What I want is to have a newfield with only value "example".
newfield does not exist in the table, but when I run this query it kinda makes a virtual column with the values of example field.
select field1, field2, 'example' as TempField
from table1
This should work across different SQL implementations.
You mean staticly define a value, like this:
SELECT field1,
field2,
'example' AS newfield
FROM TABLE1
This will add a column called "newfield" to the output, and its value will always be "example".
I'm rusty on SQL but I think you could use select as to make your own temporary query columns.
select field1, field2, 'example' as newfield from table1
That would only exist in your query results, of course. You're not actually modifying the table.
In this example, the TABLE registrofaena doesn't have the column called minutos. The query returns a column "minutos" with demora/60 as the content (the values represent the delay in minutes). The table is not modified in the process.
This is the query:
SELECT idfaena,fechahora,demora, demora/60 as minutos,comentario
FROM registrofaena
WHERE fecha>='2018-10-17' AND comentario <> ''
ORDER BY idfaena ASC;
This is the view:
select field1, field2, NewField = 'example' from table1
select field1, field2, '' as newfield from table1
This Will Do you Job
I have 2 tables:
tab1 (field1, field2, field3)
tab2 (field1, field2,field3, field4)
I want to copy a record from tab1 to tab2 taking all the fields and adding a value for field4.
How can I select field1, field2 and field3 from tab2 and also add a value? I know that SELECT and VALUES in a INSERT query are mutually exclusive.
Thanks in advance.
Gustavo.
I don't know Oracle, but in Ms SQL it works like this:
insert into tab2 (field1, field2, field3, field4)
select field1, field2, field3, 'New Value' from tab1