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>)
Related
I have a mysql table contains lot's of records. my table has a varchar field and a timestamp field. (I have one record for every minute)
I want to select records like this:
1,3,5,7,9,11,...
or 1,4,7,10,13,..
or something like this.
I can get done it using php while function, but it is not a good solution. is there any mysql select parameter to get it exactly from mysql?
p.s: sorry for post title, this is the only title stackoverflow accept it.
select * from table where identity_column %2 <>0 -- to select 1,3,5,7,9...
and for your 2 condition do this !
select * from table where identity_column%3 =1 -- to select 1,4,7,10,13,....
For selecting records like 1,3,5,7,9,11,etc. You can do this:
SELECT *
FROM TableName
WHERE autoIncreamentField % 2
NB: Not necessary to check where clause against 0 or 1. It will select records if where clause returns 1. An example in Fiddle.
For records like 1,4,7,10,13,etc. You can do:
SELECT *
FROM TableName
WHERE (autoIncreamentField % 3)=1
select * from table order by rand()
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
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
Is there a way to return the Mysql orderBy ranking as part of the resulting record?
For example, lets say I have a comment table where I am querying results ranked by:
comment_rating and comment_length, using orderBy('r.comment_rating*r.comment_length').
Now I want the resulting records to include the value of their respective comment_rating*comment_length calculations.
Is this possible?
edit: also, does doctrine perform the ranking calculations twice if I do this and also use the same algorithm for orderBy?
Do you mean something like:
SELECT *, (comment_rating * comment_length) AS ranking FROM comment ORDER BY ranking DESC
Edit
Haven't used Doctrine, but after a quick glance at the documentation, I guess it would be something like this:
$q = Doctrine_Query::create()
->select('*, (comment_rating * comment_length) AS ranking')
->from('comment')
->orderBy('ranking');
$comments = $q->execute();
Try this :
Select comment_rating, comment_length,
(comment_rating * comment_length) as rat_len
From comment
OrderBy rat_len
All you need to do is include
(comment_rating*comment_length) as comment_ranking
in the SELECT field list.
SELECT
comment_rating,
comment_length,
comment_rating*comment_length AS comment_rank
FROM
tablename
ORDER BY
comment_rank;
Try this:
SELECT <yourFields>, (r.comment_rating * r.comment_length) AS Rating FROM ...
Documentation:
Doctrine Query Language: Aggregate-values.
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.