Suppose there is a SQL statement:
select * from A order by cola
In sqlalchemy, we can use this code:
session.query(A).order_by(asc(cola))
Now I want to use a "compound order by" in SQL:
select * from A order by cola, colb
Then how will I translate it into sqlalchemy code? Can I use:
session.query(A).order_by(asc(cola, colb))
Probably I can't do it like this.
I find I can do this:
session.query(A).order_by('cola, colb')
Then this problem will be solved.
Related
This problem began when I was trying to set the order by column from a bound statement.
This is not working for me:
select * from testTable order by field(?, 'userid','name') asc;
The field() function is not being evaluated for some reason.
After further investigation I found that this also does not work:
select * from testTable order by (1+1) asc
Are these both the same problem? And what is the solution?
I'm just guessing, but I think it is plausible that you can't use ORDER BY #ColumnIndex (For example ORDER BY 2) when you are using SELECT *.
Try specifying all the columns you are willing to select, or use column names in the order by clause.
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>)
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.
I ran a query that resulted in the string '1,2,3,4'.
How can I run a second query that treats that string as a list of numbers. So I'll be able to do:
select * from tbl where name not in (1,2,3,4)
I would like an answer in pure MySQL.
Well first of all, this usually means that your database structure is not good; you should normalize your database.
However, you can do what you want, with the FIND_IN_SET function:
SELECT * FROM tbl WHERE NOT FIND_IN_SET(name, '1,2,3,4')
Use FIND_IN_SET:
select * from tbl where FIND_IN_SET(name, '1,2,3,4') = 0
Like the other answer, I would also recommend normalizing your database if at all possible. This query could be slow as it will require a scan of the table. Even if there is an index on name this query won't be able to use it efficiently.
Right now I'm retrieving data from my database as follows:
SELECT id, UNIX_TIMESTAMP(cdate) as myunixdate, permalink, title FROM mytable
But I would like to do it as follows, but it doesn't work.
SELECT *, UNIX_TIMESTAMP(cdate) FROM mytable
My question is, how can I combine UNIX_TIMESTAMP without having to specify all the other fields?
Are you sure you didn't try this?
SELECT UNIX_TIMESTAMP(cdate), * FROM mytable
This won't work as the * has to come first:
SELECT *, UNIX_TIMESTAMP(cdate) FROM mytable
Aliasing it will make it easier to reference in your code:
SELECT *, UNIX_TIMESTAMP(cdate) AS cdate_timestamp FROM mytable
SELECT *, UNIX_TIMESTAMP(cdate) AS my_time_stamp FROM mytable
It works for me in MySQL 6,
Are you sure the second query is the one you really try?
What version of mysql do you use?