MySQL if / else or select if(contiton,'true','false'); - Syntax-problems - mysql

When I use
select if (1=1,'true','false');
then everything is ok and I receive 'true' but if I try
select if (1=1,(select * from table1), (select * from table2));`
I receive syntax error in mysql!
With
if condition then <SQL-Expression1> else <SQL-Expression2> end if;
I have the same problem, when the SQL-Expression is complex like
select * from table1!
If I use a complex SQL-Expression like
select * from table1
or
insert into table1 (select field1 from table2 where
field1>(select Max(field) from table1));
then I always receive a syntax error, when such a expression is included in an if/else-Statement!
How can I fit it, that complex sql-Statements can be choosed?
My problem is:
I made 2 tables like
create table1 (x1 int4, x2 int4, x3 int4);
create table2 (x int4);
insert into table1 values(1,2,3);
insert into table1 values(4,0,5);
I wanted to transponse table1 to table2
For example:
The result should in table2 like this
1
2
3
4
5
If I enlarge table 1 with a new line like
insert into table1 values (6,7,8);
then table2 should be changed to
1
2
3
4
5
6
7
8
I tried to make it in this way
select if ((select count(*) from table2)=0,
(insert into table2 (select x1 from table1 where x1>0)),
(insert into table2 (select x1 from table1 where
x1>(select Max(x) from table1))));
The same also with x2 and x3.
but syntax errors occur!
If I use only
insert into table2 (select x1 from table1 where x1>(select Max(x) from table1));
then it works if table1 ist not empty otherwise I had to do
insert into table2 (select x1 from table1 where x1>0);

Subqueries in the SELECT clause may only return single values; i.e. not more than one result, and not more than one column. You can't "trick" a query into returning a varying number of columns.
Also, the if-then form of "if" is reserved for procedural sql; used in stored procedures, triggers, etc...

Related

Mysql - reference an external field from a nested subquery

How is possible to reference an external field from a nested subquery?
I'll explain better very fastly with an example:
SELECT
x
, (SELECT t1.x) as x1
/*, (SELECT x FROM (SELECT t1.x) as t2) as x2*/
FROM
(SELECT 1 as x UNION SELECT 2 as x UNION SELECT 3 as x) as t1;
If I uncomment the commented subquery, I get a "Unknown table 't1' in field list" error but I need to refer for a complex calculation to that variable from a 2-level nested subquery (it's not possible to do with a Join).
This is not possible according to MySQL manual. You can try using VIEWs instead of derived tables or list all your derived tables in the outermost FROM clause
You can't use table alias in subquery, because it is out of scope. You should (eventually) recode the subquery like this:
SELECT
x,
t1.x as x1,
(SELECT x
FROM (SELECT 1 as x
UNION
SELECT 2 as x
UNION SELECT 3 as x) as t2) as x2
FROM
(SELECT 1 as x
UNION
SELECT 2 as x
UNION
SELECT 3 as x) as t1;
or create a proper view and use the view for access to the data
create view my_view as
SELECT 1 as x
UNION
SELECT 2
UNION
SELECT 3
;
SELECT x
, t1.x as x1
, (SELECT x FROM my_view as t2) as x2
FROM my_view as t1
;

SQL to calculate sum of column values and repeat this value over all rows [duplicate]

This question already has answers here:
MySQL combining COUNT, MAX and SUM
(3 answers)
Closed 5 years ago.
Let's say I have a table like this:
COL1 COL2
ABC 1
DEF 2
GHI 3
And I want to have the sum of COL2 in a new column COL3:
COL1 COL2 COL3
ABC 1 6
DEF 2 6
GHI 3 6
So taking a simple sum won't work because it will return only 1 value, whereas I want the value to be repeated over all rows.
Try this:
SELECT *
,(SELECT SUM(Col2) FROM Your_Table) Col3
FROM Your_Table
Try this in SQL Server:
SELECT *
,SUM(Col2) OVER(ORDER BY(SELECT NULL)) Col3
FROM Your_Table
Since you didn't specify a dbms, this should work in most (all?) dbms's (tested in sql server):
DECLARE #T TABLE (Col1 VARCHAR(3), Col2 INT)
INSERT #T (Col1, Col2)
VALUES ( 'ABC' , 1 )
, ('DEF', 2)
, ('GHI', 3)
SELECT *
FROM #T
CROSS JOIN (
SELECT SUM(T.Col2) Col3
FROM #T AS T
) X
Though you didn't mention the Server Tag for SQL, I resolved it using MYSQL Server. Try this in MYSQL Server:
Select t.Col1, t.Col2, t1.Col3 from tbl as t join (Select Sum(Col2) as Col3 from tbl) as t1
See the demo here
In order to add the two columns you have to perform the joins query. Base on your operation rather you have to perform inner join or outer join .For more description you can see the respective databases

MYSQL Union a limit applied to second statement will not return records

Have got a little stuck on a query. I've simplified it down to it's essence.
1st statement is the select before the UNION ALL
2nd statement is the select after the UNION ALL.
Objective: to return all results in the 1st statement and then limit the results of the 2nd statement.
What's happening: If I ask for the limit of 1 then it will not show me any records from the 2nd statement. But will show the 2 records of the 1st statement.
Anyone know how to solve this?
CREATE
OR REPLACE VIEW qrytest
AS (
select *
from
(select field1,field2
from tableOne) as tbl1
UNION ALL
select *
from
(select field1, field2
from tableOne limit 1) as tbl2
);
select *
from qrytest
where field2 = 10
Observe your CREATE VIEW statement; I don't think you need the second select statement as shown below since you are just duplicating a record in that table.
select *
from
(select field1, field2
from tableOne limit 1) as tbl2
Your query can be modified to
CREATE OR REPLACE VIEW qrytest
AS (
select field1,field2
from tableOne
);

Mysql divide multiple column with 2 statement

I would like to divide multiple column with 2 statements as the following:
TBL1
NAME VAL1 VAL2 VAL3
A 2 3 3
TBL2
NAME VAL1 VAL2 VAL3
B 2 3 3
ERROR SCRIPT
select (select * from tbl1)/(select * from TBL2) as result
Result that i need as the following:
VAL1 VAL2 VAL3
2/2 3/3 3/3
There should be a ON clause but not sure what it should be
SELECT t1.VAL1/t2.VAL1, t1.VAL2/t2.VAL2, t1.VAL3/t2.VAL3,
FROM TBL1 t1, TBL2 t2
The best thing that I can come up with is
SET #COUNTER1 = 0;
SET #COUNTER2 = 0;
SELECT T1.VAL1 / T2.VAL1,
T1.VAL2 / T2.VAL2,
T1.VAL3 / T2.VAL3
FROM (SELECT *, (#COUNTER1 := #COUNTER1 + 1) AS id FROM TBL1) AS T1
INNER JOIN (SELECT *, (#COUNTER2 := #COUNTER2 + 1) AS id FROM TBL2) AS T2
ON T1.id = T2.id
Select Tbl1.Val1 / Tbl2.Val1 As Val1
, Tbl1.Val2 / Tbl2.Val2 As Val2
, Tbl1.Val3 / Tbl2.Val3 As Val3
From Tbl1
Cross Join Tbl2
Of course, this probaly isn't what you want. Firstly, there is nothing that correlates the rows in Table 1 with the rows in Table 2. I.e., if both tables have three rows each, the result will have nine rows. In short, you will get a Cartesian product between the two tables. Second, there is no logic to deal with dividing by zero errors. Should those values simply be set to zero? Should they be null?
MySQL Join Syntax. (Yes MySQL supports the ISO/ANSI standard Cross Join syntax).
SQL Fiddle version
Edit
If what are trying to do is to concatenate the values into a string expression of #/#, then you need to use the Concat function:
Select Concat(Tbl1.Val1,'/',Tbl2.Val1) As Val1
, Concat(Tbl1.Val2,'/',Tbl2.Val2) As Val2
, Concat(Tbl1.Val3,'/',Tbl2.Val3) As Val3
From Tbl1
Cross Join Tbl2
SQL Fiddle version.

find the output mysql query

I have a table name T1 having only one Column name Col1 having rows –
Col1
a
b
c
And another table name T2 also having only one Column name Col1 having rows –
Col1
x
y
z
Now I want record like
Col1--Col2
a------x
b------y
c------z
I am using mysql.
Thanks in advance!!
create table T1(col1 varchar(10));
insert T1 values ('a'),('b'),('c');
create table T2(col2 varchar(10));
insert T2 values ('x'),('y'),('z');
select A.col1, B.col2 from
(select #r:=#r+1 rownum, col1 from (select #r:=0) initvar, T1) A,
(select #s:=#s+1 rownum, col2 from (select #s:=0) initvar, T2) B
where A.rownum=B.rownum
Because there is no ORDER BY clause, you are depending on luck and convention for the row numbering to be according to the order inserted. It may not always be the case.
In your example, if you want to join the tables to get row results like this:
Row 1 - A,X
Row 2 - B,Y
Row 3 - C,Z
..then you will have to add a common field that you can JOIN the two tables on.
If you want to be able to return results from both tables like this:
Row 1 - A
Row 2 - B
Row 3 - C
Row 4 - X
Row 5 - Y
Row 6 - Z
.. then you will need to use a UNION:
(SELECT Col1 FROM T1) UNION (SELECT Col1 FROM T2)