I have a issue with the if statement in mysql - mysql

I want to execute a if statement which is like this
select IF ( quantity_wanted!=3,(select name from grocerywanted),Null) AS message from grocerywanted;
I want here to check the quantity_wanted column if it contains a value except 3 run the select statement

Your query is using same table name not as sub query. I think you should use 'name' in replace of '(select name from grocerywanted)'. You will get your desired output.
select IF ( quantity_wanted!=3,name,Null) AS message from grocerywanted;

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;

Sql Unknown column error 'where clause'

I want to run a SQL query (or multiple queries) on database of my website in phpMyadmin in the SQL tab it says
SELECT * FROM `shell` WHERE 1
SO I typed in
SELECT * FROM `shell` WHERE 'http://samiul.ru.cx/c0de.php';
but I get an error
1054 - "Unknown column 'http://samiul.ru.cx/c0de.php' in 'where clause'
The shell should be an existing table on your phpMyadmin.
After the WHERE statement you should place your column name that you want to filter. For example your table shell contain a column name shell_name with a character data type. Your sql statement should look like:
SELECT *
FROM shell
WHERE shell_name like 'any_name';
If your not planning to filter your table you can remove the WHERE statement.
Leaving you only the main sql statement:
SELECT * FROM shell;
The general syntax for a select query with where clause is:
SELECT * FROM TABLE_NAME
WHERE COLUMN_NAME = 'PARTICULAR_COLUMN_VALUE';
Where clause is used to filter records that satisfy a particular filter criteria.
For example, You have a table say EMP_TABLE with following structure and records,
EMP_NAME | EMP_NUMBER
NISHA | 3322
GRASHIA | 3696
If you want to fetch all columns of all records that have name as 'NISHA', then your query should be:
SELECT * FROM EMP_TABLE WHERE EMP_NAME = 'NISHA';
*Note: * in the above query can also be expanded as SELECT EMP_NAME, EMP_NUMBER where you can explicitly mention all the column names
If you want to fetch only particular column say emp number of employee 'NISHA' then your query can be:
SELECT EMP_NUMBER FROM EMP_TABLE WHERE EMP_NAME = 'NISHA';
For more reference on Syntax of WHERE CLAUSE
SELECT * FROM *table* WHERE *column* = *value*
In your case:
SELECT * FROM shell WHERE webpage = 'http://samiul.ru.cx/c0de.php'
Or similar.
The WHERE 1 part is a little confusing.
SELECT * FROM table WHERE 1
is functionally the same as
SELECT * FROM table
Here's an explanation: Importance of WHERE 1 in MySQL queries
I've never actually had a need for it, but maybe someone has.
This is a pretty basic question. You might want to read up a little on how queries work.

Grouping two mySQL select statements under one alias and ordering that alias

I want to run two mysql SELECT statements, combine them, call the new combination by its own name, then order that new combination by a user-defined function. This is what I am trying currently:
SELECT * FROM (
SELECT * FROM dictionary WHERE def1 LIKE '$input%'
UNION ALL
SELECT * FROM dictionary WHERE def2 LIKE '$input%'
) AS newcol
ORDER BY levenshtein('$input', newcol)
LIMIT 10
But I get the following error:
Unable to run query:Unknown column 'newcol' in 'order clause'
The problem is clearly with defining the new group 'newcol'.
You're trying to order by a TABLE, not by a FIELD! Use a field from your tables ant it'll go smooth

How to get minimum value from a set of values returned by SELECT query?

I have written a SELECT query, which will return a set of values, for ex.,
The following one is the actual table -
select data from tab1 where id <5; // This statement returns me the following table
I am trying to get the minimum value of the resultant table. I have tried the following query for that -
select MIN(select data from tab1 where id<5);
SQLite Browser says, there is an error in the select statement. My doubt is, whether we can give a select statement directly into an aggregate function like MIN()? If not, can you please suggest me a better way to do this task?
Thanks in advance.
Try this way:
select MIN(data)
from tab1 where id<5;

Pass number as a column name in select statement of Sql

My query is like this
select 5 from mytable_name;
Then the output is like column name 5 and the value is 5 printing as many max number of rows exists in that table.
Can anybody tell the reason why this query is working like this?
Can anybody tell the reason why this query is working like this?
You are selecting a string literal value '5' for each row in your table:
select 5 from mytable_name;
And this works fine. Because in the SELECT statement you can select:
Column reference,
Literal value like in your case.
Function.
value expression.
Select expression.
As defined by the standard SQL1:
Update:
However, If you have a column with a name is a number like in your case, you have to escape it in order to select the values in it like so:
SELECT `143` FROM Table1;
This will select all the rows in the column 143.
But, this:
SELECT 143 FROM Table1;
Will select the string literal 143 for each row found in the table.
Note that: If possible, try not to name these columns this way, it is recommended and a best practice, not to do this.
SQL Fiddle Demo
Update 2:
Note that, if you select 143 or '143', or even "143" this will select the literal value 143 not the column date. The following are the same:
SELECT 143 FROM Table1;
SELECT '143' FROM Table1;
SELECT "143" FROM Table1;
All these SELECTs won't select the data in the column, They will select the literal value 143 not the column data. See this demo:
Demo
You have to escape the column name with the two:
``
Like this:
SELECT `143` FROM table1;
Not:
SELECT '143' FROM table1'
Like what I did here:
The right Demo
1Image From: SQL Queries for Mere Mortals
from mytable
will select all rows from your table if there is no where condition that shrinks that result. and
select 5
will select the constant number 5 for every record. If you use a column name in the select part then that value will be selected for every record.
The DB engine will name the result 5 because it automatically generates a column name and 5 is the logical name for that.
You want 'SELECT * FROM mytable_name LIMIT 0,5' perhaps?
Since you don't have anything in your where clause, it is selecting all the rows from your table. The fact that you don't select any of the columns is irrelevant - you'll still get a result for each row in the table.
With the command select 5 ... you are viewing a fixed value. Same thing you run the following command: select "test", you will be displaying a fixed string.
Using ... from mytable_name you're looking for all record of this table.
With this we can conclude that for each record in the table mytable_name shows you the fixed value "5".