amazon simpleDB query - php

i am trying to create a query on my simpleDB. here is the query:
select * from flyers where eventDate >= '20120101' and clubId= '0001' OR clubId = '0002' ORDER BY eventDate asc limit 20
the problem is with:
clubId= '0001' OR clubId = '0002'
i get the error: [Invalid sort expression. The sort attribute must be present in at least one of the predicates, and the predicate cannot contain the is null operator.]
i would also like to be able to have up to 20 'OR''s chained together if it is possible

as daan kindly pointed out:
select * from flyers where eventDate >= '20120101' and clubId in('0001','0002') ORDER BY eventDate asc limit 20

Related

How to order non-integer records in MySql?

I'm trying to order some MySQL records by DESC order and get the last record added.
These are my records and they're all stored as strings:
1/2017
1/2018
2/2017
2/2018
3/2017
I want to get the value 1/2018. The records are based on the current year
and the last record added was 1/2018 and the next will be 2/2018 and so on.
this is my query:
SELECT NoControl FROM cita ORDER BY NoControl DESC LIMIT 1
but I'm getting 3/2017 as the last record.
If you have ever the same pattern m/yyyy you could use some string manipulation function eg:
SELECT NoControl
FROM cita
ORDER BY right(NoControl,4), DESC left(NoControl,1) DESC LIMIT 1
But you should avoid of store date values as string ..
or convert the string as a proper date
SELECT NoControl
FROM cita
ORDER BY str_to_date(NoControl,'%m/%Y') DESC LIMIT 1
Use SUBSTRING_INDEX to extract the first and second part, CAST as integer and sort:
SELECT str
FROM testdata
ORDER BY
CAST(SUBSTRING_INDEX(str, '/', -1) AS UNSIGNED) DESC,
CAST(SUBSTRING_INDEX(str, '/', 1) AS UNSIGNED) DESC
LIMIT 0, 1
SQL Fiddle

mysql union statement failing because of bracket placement?

I have written three working sql statement, and am trying to use a union like this:
(SELECT * FROM articles WHERE date <= '2015-01-15' AND category='News' )
UNION
(SELECT * FROM articles WHERE date <= '2015-01-15' AND subcategory='News')
ORDER BY date DESC LIMIT 5
UNION
SELECT * FROM articles WHERE Month(recurring) = '01' AND category='News' ORDER BY
recurring DESC LIMIT 5
The basic structure is what I'm asking about: two initial queries unioned and sorted together by date.
Then a third query, sorted by a separate column, and then unioned into the first two.
The query above seems to work fine when I sort the first 2 queries inside the brackets separately, but fails once I take it out of the brackets and sort them together.
Is sorting by a separate column causing it to fail? I tried adding an additional set of brackets to encapsulate the first 2 statements plus the sort order, but no go.
Here is the error code:
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 'UNION SELECT * FROM articles WHERE Month(recurring) = '01' AND category='News' at line 2
There are a few problems with your query. An UNION query can only have one LIMIT clause and one ORDER BY clause, and both are applied to the whole result.
If I understand your logic correctly, I would rewrite your query as this:
(
SELECT * FROM articles
WHERE date <= '2015-01-15' AND (category='News' OR subcategory='News')
ORDER BY date DESC
LIMIT 5
)
UNION
(
SELECT * FROM articles
WHERE Month(recurring) = '01' AND category='News'
ORDER BY recurring DESC
LIMIT 5
)
if your query is more complicated and you really want your first two queries to be separated, this is the right syntax to use:
SELECT * FROM (
SELECT * FROM articles WHERE date <= '2015-01-15' AND category='News'
UNION
SELECT * FROM articles WHERE date <= '2015-01-15' AND subcategory='News'
ORDER BY date DESC
LIMIT 5
) AS s
UNION
(
SELECT * FROM articles
WHERE Month(recurring) = '01' AND category='News'
ORDER BY recurring DESC
LIMIT 5
)

mysql select n rows before and n rows after

I am trying to create a mysql query to select let's say 5 rows before and 5 rows depending on the currend ID. Here is the query I am trying to use:
SELECT * FROM posts WHERE PID<='10' ORDER BY PID DESC LIMIT 7
UNION ALL
SELECT * FROM posts WHERE PID>'10' ORDER BY PID ASC LIMIT 6
For some reason though, the query is not working. Am I missing something? I found this solution in a forum post.
Try this:
(SELECT *
FROM posts
WHERE PID <= '10'
ORDER BY PID DESC LIMIT 7
)
UNION ALL
(SELECT *
FROM posts
WHERE PID > '10'
ORDER BY PID ASC LIMIT 6
)
ORDER BY PID ASC;
sqlfiddle demo
The PID is likely of int type. So you should be using the following (SQL Fiddle):
(SELECT * FROM posts WHERE PID <= 10 ORDER BY PID DESC LIMIT 7)
UNION ALL
(SELECT * FROM posts WHERE PID > 10 ORDER BY PID ASC LIMIT 6)
As you can see the ordering of the two queries is also maintained separately.
DOCS
To apply ORDER BY or LIMIT to an individual SELECT, place the clause inside the parentheses that enclose the SELECT:

How do I sort the following table & get those top5- 5 recoreds,top20- twenty records?

This query giving strange result:
SELECT `user_id`,`rankType`
FROM `ranks`
WHERE `user_id` =23
AND (`rankType` = "top5"
OR `rankType` = "top20")
ORDER BY rankType
LIMIT 0 , 30
here the SQLfiddle.
Want I am trying to achieve is:
1)To get only 5 records of top5 rank type, 20 records of rank type top20
2)I want to show the result in ascending order of rank type.(but if you see in the demo fiddle it's showing apposite, may be it is only considering 2 from 20 & 5)
(SELECT `id`,`user_id`,`rankType`
FROM `ranks`
WHERE `user_id` =23
AND `rankType` = "top5"
ORDER BY rankType
LIMIT 0, 5)
union
(SELECT `id`,`user_id`,`rankType`
FROM `ranks`
WHERE `user_id` =23
AND `rankType` = "top20"
ORDER BY rankType
LIMIT 0, 20)
If later on you want to add another set of sorting/filtering columns, wrap it all into something like
select * from ( /* previous query goes here */ ) tt
where id > 100
order by id
Note that ranktype is varchar, so it's sorted lexicographically, so top20 < top5. You'll have to employ natural sorting or some other means to get it right.
SELECT `id`,`user_id`,`rankType`
FROM `ranks`
WHERE `user_id` =23
AND `rankType` = "top5" limit 5
union
SELECT `id`,`user_id`,`rankType`
FROM `ranks`
WHERE `user_id` =23
AND `rankType` = "top20" limit 20
Your result is actually in ascending order, since column rank_type is of varchar type top20 comes first than top5 as in string comparison.
If you only want to deal between top5 and top20, a dirty solution could be:
ORDER BY rankType desc
One possibility without doing two queries and UNIONing them:
ORDER BY FIND_IN_SET(rankType,'top5,top20')

Select on Mysql inverse order

i have a MySql table that consists of 2 basic things:
The id and a value.
To show that on my page, i need to select, for example, the last 100 rows on reversed order.
So imagine that someone is putting data on it:
Id, value
1, 10
2, 9
3, 21
4, 15
i need, to select the last "3" rows (LIMIT + ORDER Clause), but not like this: 4,3,2 but like this: 2,3,4.
I know how to do that on code, but maybe there is a simple solution for that on Mysql and i don`t know.
Thanks
My SQL Query is like this right now:
SELECT `Data`.`id`, `Data`.`log_id`, `Data`.`value`, `Data`.`created` FROM `control_panel`.`datas` AS `Data` WHERE `Data`.`id` > 1000 AND `Data`.`log_id` = (2) ORDER BY `Data`.`id` DESC LIMIT 100
You need to wrap the first ORDER BY in a subselect which will return a limited selection ordered in descending order, then you can order that result in the outer query in ascending order:
SELECT
a.*
FROM
(
SELECT id, value
FROM tbl
ORDER BY id DESC
LIMIT 3
) a
ORDER BY
a.id
One way to do this would be with a sub-select.
SELECT *
FROM (SELECT * FROM table_name ORDER BY id DESC LIMIT 3) tmp
ORDER BY id ASC
simply
SELECT t.*
(SELECT * FROM table_name
ORDER BY column_name DESC
LIMIT 0,3) t
ORDER BY t.column_name ASC
use DESC to descending order, ASC to increasing order

Categories