Subquery inside IF() function - mysql

I have an error in my SQL code:
SELECT IF(1>2,select * from table2,select * from table1)
this make following error
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select * from table2,select * from table1)' at line 1
i dont know why this;
i have multiple condition based query but i cant run simple if statement please help

try this:MAKE SURE YOU ARE USING THE RIGHT DATABASE WHICH INCLUDES THESE TWO TABLES(table1, table2)
DELIMITER |
CREATE FUNCTION dataselect()
BEGIN
CASE 1>2
WHEN 1 THEN select * from table2;
WHEN 0 THEN select * from table1;
END CASE;
END;
|
and then call it :
CALL dataselect();

Related

Tried using CTE on Hackerrank, it's not working, what could be the alternate solution?

Just using this simple code to create CTE, in the end I want to create more complex code, that's why I am using CTE.
Problem Link
WITH TOTAL_SUBMISSIONS AS(
SELECT * FROM View_Stats)
SELECT * FROM TOTAL_SUBMISSIONS;
I get this Error
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TOTAL_SUBMISSIONS AS(
SELECT * FROM View_Stats)
SELECT * FROM TOTAL_SUBMISSIONS' at line 1
Does anyone know how to resolve this issue?
Put the CTE into a subquery:
SELECT TOTAL_SUBMISSIONS.*
FROM (SELECT * FROM View_Stats) AS TOTAL_SUBMISSIONS

10.3.23-MariaDB QUERY Syntax Error, INTO OUTFILE, help me solve it

So I'm trying some MYSQL queries on my linux machine, but this query won't work
MariaDB [exploit]> select * from items union select 'Hello World',null,null,null,into outfile '/tmp/test.txt';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'into outfile '/tmp/test.txt'' at line 1
What am I doing wrong here?
As a starter: the INTO clause goes before the FROM clause. It is a bit tricy to use with UNION: you need to put it in the last UNION member (and the entire result from the query is written to the out file).
So:
select * from items
union all select into outfile '/tmp/test.txt' 'Hello World', null, null, null
For more information, see the documentation:
SELECT ... INTO
UNION restrictions

Why do you have to count after 'select *' on MySQL?

So, today I tried doing the following:
SELECT count(users), * FROM table;
And it gave me a syntax error on the , after count(users), but this:
SELECT *, count(users) FROM TABLE;
Or this:
SELECT count(users), users FROM table;
Does work. Is there a reason for that?
Edit: Per request, here's the error:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* from mygamedb' at line 1
MySQL Workbench underlines the comma after the count(*). I'd like to point out I know it's an ugly select, I should group by and such, but the question is not about how to make it work, but a short example asking about why does that happen, not how to make it work.
You could use alias to make it work:
SELECT count(users), t.*
FROM table t;
DBFiddle Demo
SELECT count(users), *
FROM table;
-- You have an error in your SQL syntax;
-- check the manual that corresponds to your MySQL server version for the right
-- syntax to use near '* FROM tablez' at line 1
I am trying to explain you when you will use this SELECT count(users), * FROM table; query then mysql query fetch table as like
Table :
---count(users)---
result of count
Then you are adding * means fetch all attribute value but now mysql check * from fetch table but that is not their because table has only one attribute that name is count(users) that's why it show error. but when you will use t.table (t is alias of table) that fetch from table and it works fine.
I am commenting here what is my understanding.
Thanks

MySQL how to use CONCAT in IN condition?

This SQL works ok:
SELECT * from table_name where id IN (473,473,475);
This doesn't:
SELECT * from table_name where id IN CONCAT('(', '473,473,475', ')');
It says:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'CONCAT('(', '473,473,475', ')')' at line 1
Why?
I want to use it something like this:
SELECT * from table_name where id IN CONCAT('(', ids, ')');
ids is varchar and contains something like this:
473,473,475
Use FIND_IN_SET:
SELECT *
FROM table_name
WHERE FIND_IN_SET(id, ids);
The parameter to IN must be either a literal list or a subquery. CONCAT() returns a string, not a list -- SQL doesn't re-parse the result.

does the existence of an asterisk in a select exclude other columns?

This question is all about laziness... I'd like to do something like this:
select some_func(some_col), * from my_table
So that I don't have to do this:
select some_func(some_col), col_1, col_2... col_ad_infinitum from my_table
Is there any way to make the first query work? This is the error I get when I run it:
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* from my_table' at line 1
Do you mean that in MySQL your first query:
SELECT some_func(some_col), *
FROM my_table
produces this error?:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*' at line 1
You can change your code into (this results in no errors!):
SELECT *, some_func(some_col)
FROM my_table
or into this, if you want to have the calculated columns first:
SELECT some_func(some_col), t.*
FROM my_table AS t
Unfortunately, mysql only supports the asterisk at the start of the column list (unlike every other DB I am familiar with)
(Edited: start not end - oops!)
Change the order of your select params:
select *,some_func(some_col) from my_table
Anyway, as the Zen of Python says: "Explicit is better than implicit". Always try to write the fields you're selecting, and if it's posible try to put the table they're from too, you can use an alias. Your future YOU will thank you.
select t.some_col from my_table t
When I do that with PostgreSQL, I get the column(s) I specify followed by all the other columns (possibly repeating the column(s) I specified).