MySQL how to use CONCAT in IN condition? - mysql

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.

Related

Get unique values of a field and prints its length

Table Name: Worker,
Fields : worker_id | first_name | last_name | department
I have a table name worker and i wanted to write an SQL query that fetches the unique values of DEPARTMENT from Worker table and prints its length. So i tried running this : (Database- Mysql)
select length(distinct(department)) from worker;
But it is giving an error saying:
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 'distinct(department)) from worker' at line 1
But when i ran below query, it works perfectly fine:
select distinct(length(department)) from worker;
Can somebody please explain me why interchanging distinct and length function makes query works?
Thanks in advance!
Try not to use distinct like function but clause otherwise it will give syntax error.
Below sql statement will execute as shown below:
select distinct (length('xyz')) ---- length('xyz') : 3
select distinct (3) ---- output : 3
Distinct is not properly a function but a clause
select distinct length(department) from worker;
Anyway in MySQL work also with function syntax
select distinct( length(department)) from worker;
The code with the exchanged token don't work because DISTINCT produce an aggregated result removing the duplicated values,this implies that the outer length() function work on not correct set of rows or better the db engine see that there an improper use of the DISTINCT clause and raise the syntax error
select length( distinct 'A' ) this raise an error
If you want use the outer length() function you should code this way
select length(my_col) from (
select distinct department my_col from worker
) ;
correct answer :
select distinct <column_dept> as department***,*** (len(<column_dept>) as length_column_dept from xyz_table
select distinct( length(department)),department from worker group by department;
select distinct (Department) as 'Unique department', len(Department) as 'length of name' from Worker;

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

Subquery inside IF() function

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();

MySQL syntax error near "from (subquery) 'table'"

select *
from ( select * from table ) 'table1';
I cannot see why I am getting this error:
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 ''table1'' at line 2
I have checked the manual (MySQL subquery in FROM clause) and I can't see any difference between the examples and my little statement.
Table names/aliases must either be surrounded with backticks or nothing
select *
from ( select * from table1 ) table1;
I think you want back quotes rather than forward quotes:
select *
from ( select * from table ) `table1`;
Forward quotes specify a string constant. Back quotes delimit a name.
when you have a subquery that is a table, you need to declare it with a name.
select * from (select * from table1) as x
As well as not putting quotes around the alias, I believe you also need backticks around "table" in the subquery because it's a reserved word in MySQL (assuming you did actually name your table "table"):
select * from ( select * from `table` ) table1;