using of NOT LIKE in MYSQL & getting error of SubQuery - mysql

I have used below mysql query for fetching data:
select *
from tableName
where tableName.field_type='22'
and tableName.field_id NOT LIKE(select aField_id
from TableName 02 where status !='Active')
I am getting error
1242 - Subquery returns more than 1 row
can you let me know what wrong in this query

select * from tableName
where tableName.field_type='22'
and
tableName.field_id
NOT IN(select aField_id from TableName where status !='Active')
Use not in in place of not like. Not in is for comparison of column with a set of values. not like is for comparison of column with a single value or pattern. Your subquery in returning more than one rows. not like can't handle that.

Like deals with only one input. So you should use IN in place of LIKE.

Related

How can I retrieve the column names from an empty MySQL select query result

Is there a way to retrieve the column names of a query that returns no data?
The result of this query would be empty.
Is there a way how to find the column names when there's no result?
Please note that I'm aware of solutions using DESCRIBE and select column_name from information_schema.columns where table_name='person';
but I need a more flexible solution that will fit these multicolumn queries.
Please also note that I am still using the original PHP MySQL extention (so no MySQLi, and no PDO).
If you wrap your query with the following SQL, it will always return the column names from your query, even if it is an empty query result:
select myQuery.*
from (select 1) as ignoreMe
left join (
select * from myTable where false -- insert your query here
) as myQuery on true
Note: When the results of the subquery are empty, a single row of null values will be returned. If there is data in the subquery it won't affect the output because it creates a cross-product with a single row...and value x 1 = value
Execute following command if the result of your previous query is empty
SHOW columns FROM your-table;
For more details check this.
I'm not sure if it will satisfy you but you can do this
SELECT *, COUNT(*) FROM table;
It will return null values (except last column which you can ignore) if the query is empty and you will be able to access all columns. It's not proper way of doing it and selecting names from INFORMATION_SCHEMA would be much better solution.
Please note that result is aggregated and you need to use GROUP BY to get more results if there are any.
You should ,
Select COLUMN_NAME From INFORMATION_SCHEMA.COLUMNS
Where TABLE_SCHEMA='yourdb'
AND TABLE_NAME='yourtablename';

Running a SQL SELECT statement against a MYSQL column of SET type

I'm trying to run a SQL SELECT statement against a column that is of type SET. The table is called myTable and the columns in myTable are called base_props and names. The base_props column is of type SET. The values in base_prop are vb,nt, cnt,poss and loc. So I would like to SELECT entries from the column 'name' where base_props have both the values, vb and poss. The results I'm looking to get may have values other than just vb and poss. So to be clear I would like to select all entries that have the values vb and poss regardless if they have other values as well. I've tried the following SQL queries but I can't get the desired results.
SELECT name from myTable WHERE base_props = 'vb' AND base_props = 'poss'
That query returns an empty result set. I've tried using FIND_IN_SET() and IN() but I couldn't get anywhere with that. I've written SQL statements before but never had to deal with columns that are type SET. Any help is appreciated.
The only thing I can come up with is using the LIKE keyword:
SELECT name FROM myTable WHERE (base_props LIKE '%vb%' AND base_props LIKE '%poss%');
This will make sure both vb and cnt are in the base_props column. Of course you can use cnt, nt and loc in there, or any number of base_props values in the sql, just add more AND statements.
OR as a deleted answer by samitha pointed out, you can use FIND_IN_SET:
SELECT name from myTable WHERE FIND_IN_SET('vb', base_props) AND FIND_IN_SET('poss', base_props);
Comment (by spencer7593): "both of these work, but there is a slight difference. The LIKE operator will actually match any member that includes the search string anywhere in a term; the FIND_IN_SET function will only match an exact member. It's also possible to search for members in set by the order they appear in the SET definition, using the MySQL BITAND operator: for example, to match the 1st and 4th members of the set: WHERE base_props & 1 AND base_props & 8". So for example, if you have 'a' and 'aaa' in your set, then using the LIKE "%a%" method will also return rows containing 'aaa'.
Conclusion: use the FIND_IN_SET solution since it will work for all cases.
FIND_IN_SET return index, Try this
SELECT name from myTable WHERE FIND_IN_SET(base_props, 'vb') > 0 AND
FIND_IN_SET(base_props, 'poss') > 0

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".

MySQL select with subquery having replace

So I have a data with format like ;1;;2; and then I need to use this number in a query so I thought I'd convert it to 1,2 and use that in a IN condition. In my table, the result should return 2 rows but instead it is returning only 1 row.
My query is like this. The subquery return 1,2 with no problem but only 1 row is retrieve.
select *
from wt_lists
where id IN ((select replace (replace(sendto, ';;',','),';','')
from wt_stats where statsid IN (1)))
But when I try it with this. It returns the correct result, which in my case is 2 rows.
select *
from wt_lists
where id IN (1,2)
What am I missing here?
Comma delimited strings need to be explicitly defined in the query in order to be used in the IN clause - there's countless examples on SO where people need to use dynamic SQL to incorporate user submitted comma delimited strings.
That said, I have a solution using the FIND_IN_SET function:
SELECT DISTINCT wl.*
FROM WT_LISTS wl
JOIN (SELECT REPLACE(REPLACE(ws.sendto, ';;',','),';','') AS ids
FROM WT_STATS ws
WHERE ws.statsid = 1) x ON FIND_IN_SET(wl.id, x.ids) > 0
You are replacing the string:
';1;;2;'
To:
'1,2'
So, you SQL query looks like:
select * from wt_lists where id IN ('1,2') from wt_stats where statsid IN (1)
To use IN clause you need select different values in different rows.
I found this store procedure that does exactly what you need.
http://kedar.nitty-witty.com/blog/mysql-stored-procedure-split-delimited-string-into-rows/
I have not tested, but it is the way.
Obs: Like David said in the comments above, parsing the data in your application is a better way to do this.