I have one customized function created in MySQL.
I want to call it in where condition of query like
SELECT * FROM TABLE_NAME
WHERE (FUNCTION_NAME);
Is this possible?
Note: My function returns values like a query
Yes, you can use functions on queries...
the only thing is your condition, if you are going to use something like
SELECT * FROM TABLE_NAME WHERE id = FUNCTION_NAME; be sure that FUNTION_NAME return a number and not a query, or if you are going to use where in,
SELECT * FROM TABLE_NAME WHERE id in (FUNCTION_NAME); be sure that FUNCTION_NAME return the query with one column. The important thing about the function_name is the thing that is returned, like a value, or a query, etc
Related
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';
Problematic Query:
SELECT * FROM my_links WHERE taxonomy='ait-dir-item-category'
Above query returns no record and no error.
I am getting empty result, but I have exactly same value 'ait-dir-item-category' for taxonomy column.
But following query returns result
Perfect Query:
SELECT * FROM my_links WHERE taxonomy='post_tag'
Please tell me what is the problem.
Try with LIKE statment
SELECT * FROM my_links WHERE taxonomy LIKE '%ait-dir-item-category%'
Did you tried using like statement
Can you try like this,
select * from my_link where taxonomy like '%ait-dir-item%'
I am getting some records from sorted table and would like to ask some other table for records with the same ... lets say ... id.
SELECT * FROM duckies WHERE fluffy_id IN (<array_of_fluffy_ids>) ...
Is there any way to order the query result exactly the same way as fluffy_ids in IN() clause?
Yes, there is. Use FIELD() function:
SELECT
*
FROM
duckies
WHERE
fluffy_id IN (<array_of_fluffy_ids>)
ORDER BY
FIELD(fluffy_id, <array_of_fluffy_ids>)
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;
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.