select 2 + 2 is a valid mysql statement, but in oracle I have to provide a table name select 2 + 2 from foo (or dual table). Is there any way to not set the from statement? I wonder why it is not optional in Oracle. Providing a fake table sounds dummy when it could be optional as other RDBMs.
You have to select from something; in Oracle, it is the DUAL table:
SQL> select 3 + 6 from dual;
3+6
----------
9
It is a SYS owned table and contains a single row:
SQL> select * From dual;
DUMMY
--------------------
X
Therefore, it is useful for such things (where you need to fetch a single line).
The way you put it (select ... from foo) is kind of OK, if foo also contains one row. But, what if it doesn't? Have a look: there are 4 departments in the dept table:
SQL> select 3 + 6 from dept;
3+6
----------
9
9
9
9
So - use DUAL :)
Using PL/SQL Subprograms in Oracle you can execute 2 + 2, for example
DECLARE
v_sum number;
BEGIN
v_sum:= 2 + 2;
END;
This is not related to SQL in general, but from SQL* Plus or in other ides running as script(F5), you can do arithmetic without specifying a table
VARIABLE b NUMBER
EXEC :b := 2+2;
PRINT b;
PL/SQL procedure successfully completed.
B
----------
4
use dual because it is a single row single column table with a datatype of VARCHAR2(1) in it.ALways use dual when you want to fetch single output.it has a dummy value of X.
AS wiki says :- The idea was that you could do a JOIN to the DUAL table and create two rows in the result for every one row in your table. Then, by using GROUP BY, the resulting join could be summarized to show the amount of storage for the DATA extent and for the INDEX extent(s). The name, DUAL, seemed apt for the process of creating a pair of rows from just one.[1]
The original DUAL table had two rows in it (hence its name), but subsequently it only had one row.
Related
I have multiple store procedures to do the ETL work in mysql. Normally, it is running on the server for over night.
inside the store procedures there are multiple update statement like
update table1 set column1=3 when column2 = 4
if there any way, I can keep the mysql workbench result like
Rows matched: 100 Changed: 50 Warnings: 0
for each statement I run either into mysql table or external file?
prefer mysql native method. if not, any python I could possible use?
"Rows changed" can be retrieved with ROW_COUNT() function.
"Rows matched" needs in a trick with user-defined variable usage.
CREATE TABLE test (id INT, val INT);
INSERT INTO test VALUES
(1,1), (1,2), (1,3), (2,4);
Now we want to perform UPDATE test SET val = 3 WHERE id = 1; and count the amounts.
UPDATE test
-- add user-defined variable for matched rows counting
CROSS JOIN ( SELECT #matched := 0 ) init_variable
-- increment matched rows counter (this expression is always TRUE)
SET val = CASE WHEN #matched := #matched + 1
-- update the column
THEN 3
END
WHERE id = 1;
SELECT #matched matched, ROW_COUNT() changed;
matched | changed
------: | ------:
3 | 2
db<>fiddle here
If more than one column should be updated in a query then only one expression must be accompanied with the counter increment.
Maybe I'm totally wrong and it's not pssible to do in MySQL. But, what I wanted to do is to fill out column row base in a select query:
For example:
Table 1:
IdNode NodeName NodeContact NodeStatus Nodegroup
1 Machine1 IT 1 IT
2 Machine2 IT 0 IT
3 Machine3 IT 1 IT
4 Machine4 IT 1 Others
4 Machine5 IT 1 Others
Table 2
IdGroup GroupName NodesManagedbyGroup
1 IT ??
2 others ??
Having those two tables, I would like to fill out (automatically) all rows in column NodesManagedbyGroup in the table2.
Manually it would be:
SELECT COUNT(*) FROM table1 where MemberOfGroup='IT';
result value Int = 3
Then
update table2 NodesManagedbyGroup = 3 where GroupName='IT';
There is any way that MySQL do it for me automatically
You can use triggers to do this - you'd create triggers for insert, update and delete on table 1 to update table 2.
This is generally a bad idea though - it's duplicating information around the database, denormalizing the schema.
Alternatively, you can create a view which calculates this data on the fly and behaves like a table when querying.
Use multiple UPDATE syntax with selecting counts in subquery as a sample:
UPDATE
table2
INNER JOIN
(SELECT COUNT(1) AS gcount, Nodegroup FROM table1 GROUP BY Nodegroup) AS counts
ON table2.GroupName=counts.Nodegroup
SET
table2.NodesManagedbyGroup=table1.gcount
You could set a job that performs a stored procedure every 15 minutes or so?
I would like to write a query which:
as short as possible
grammatically correct to MySQL server
makes server returning an error when executed
My current solution is:
CREATE TABLE FOO (i INT);
INSERT INTO FOO VALUES (1);
INSERT INTO FOO VALUES (2);
SELECT 1 FROM FOO WHERE 1 = (SELECT i FROM FOO);
However I don't like it - it is too long.
CREATE TABLE FOO (i INT);
CREATE TABLE FOO (i INT);
It will not accept to create the table because it already exists after the first statement.
Here are some short queries that are all "correct" but will fail
SELECT *
SELECT 1e333
SELECT i FROM(SELECT 1i,1i)
The verbose versions are
SELECT * FROM DUAL
SELECT 1e333 FROM DUAL
SELECT i FROM (SELECT 1 as i, 1 as i FROM DUAL)
They fail for different reasons:
1) DUAL does not have any columns.
2) 1e333 does not fit into a float.
3) The inner query returns a table with two identical named columns. Thus specifying this column name is not admissible for the outer query.
how to write query for following request?
my table:
id designation
1 developer,tester,projectlead
1 developer
1 techlead
if id=1,designation="'developer'"
Then need to first,second records.Because 2 rows are having venkat.
if id=1,designation="'developer','techlead'" then need to get 3 records as result.
i wrote one service for inserting records to that table .so that i am maintaining one table to store all designation with same column with comas.
By using service if user pass id=1 designation="'developer','techlead'" then need to pull the above 3 records.so that i am maintaining only one table to save all designations
SP:
ALTER PROCEDURE [dbo].[usp_GetDevices]
#id INT,
#designation NVARCHAR (MAX)
AS
BEGIN
declare #idsplat varchar(MAX)
set #idsplat = #UserIds
create table #u1 (id1 varchar(MAX))
set #idsplat = 'insert #u1 select ' + replace(#idsplat, ',', ' union select ')
exec(#idsplat)
Select
id FROM dbo.DevicesList WHERE id=#id AND designation IN (select id1 from #u1)
END
You need to use the boolean operators AND and OR in conjunction with LIKE:
IF empid = 1 AND (empname LIKE '%venkat%' OR empname LIKE '%vasu%')
The above example will return all rows with empid equals 1 and empname containing venkat or vasu.
Apparently you need to create that query based on the input from user, this is just an example of how the finally query should look like.
Edit: Trying to do this within SqlServer can be quite hard so you should really change your approach on how you call the stored procedure. If you can't do this then you could try and split your designation parameter on , (the answers to this question show several ways of how to do this) and insert the values into a temporary table. Then you can JOIN on this temporary table with LIKE as described in this article.
I want to run an
INSERT ... SELECT
Manual
query to insert 1 selected column column with the exact amount of 2 rows from one table to another.
Along with this I would like to insert an additional column of static values.
Example
| selected column | static val |
Variable 4
Variable 9
static values 4 & 9 are specified in my php script.
Can I do this in 1 mysql query, not using php to store temporary data?
You can use UNION ALL to select from the source table twice like this:
insert into new_table
select column, 4 from old_table
union all
select column, 9 from old_table;