i want something like this:
SELECT DISTINCT ID from copies WHERE timestamp < 1229444506 ORDER BY CID
the problem is that this one only return ID and i need CID and other columns in the table.
It may be that this is totally wrong for other reason aswell, so i will explain what i need.
I have a table that "record" every row-change in my maintable. This so in the future i will be able to go back in time and see how the main table looked like a certain date in time.
So what i need is a query that ORDER all rows BY CID, WHERE timestamp < 1229444506, AND then DISCTINCT them by ID.
I want to the query to return the first row by ID, when ordered by CID newest first.
Ive tried to get this working with different methods but no luck.
Any suggestions?
I am not sure if I got it correctly, but what about creating a subquery that will just select the columns? You wrote:
"I want to the query to return the first row by ID, when ordered by CID newest first."
So, let's make a subquery:
SELECT id, max(cid) as maxcid FROM copies WHERE timestamp < XX group by id
This will give you the relationship id <=> the CID you want. And now join it:
SELECT copies.* FROM copies, (SELECT id, max(cid) as maxcid FROM copies WHERE timestamp < xxx group by id) x
WHERE copies.id=x.id AND copies.cid=x.maxcid;
SELECT * FROM copies WHERE timestamp < 1229444506 GROUP BY ID ORDER BY CID DESC;
EDIT:
SELECT * FROM copies WHERE timestamp < 1229444506 GROUP BY ID ORDER BY CID;
You can also try:
SELECT * FROM copies WHERE timestamp < 1229444506 GROUP BY ID ASC ORDER BY CID;
Or:
SELECT * FROM copies WHERE timestamp < 1229444506 GROUP BY ID DESC ORDER BY CID;
Does it work?
how can your database know which rows to append to your distinct column? say you haveː
id | name
---+-----
1 | a
1 | b
now the hypothetical query: SELECT DISTINCT id, name FROM table. which name do you expect to show up in your result? a or b?
if that's not a problem you can group your result in mysql by id, but it’s unspecified which name you'll get
SELECT `id`, `name` FROM `copies` WHERE `timestamp` > 123456789 GROUP BY `id` ORDER BY `cid` DESC
Related
I want to select last 50 rows from MySQL database within column named id which is primary key. Goal is that the rows should be sorted by id in ASC order, that’s why this query isn’t working
SELECT
*
FROM
`table`
ORDER BY id DESC
LIMIT 50;
Also it’s remarkable that rows could be manipulated (deleted) and that’s why following query isn’t working either
SELECT
*
FROM
`table`
WHERE
id > ((SELECT
MAX(id)
FROM
chat) - 50)
ORDER BY id ASC;
Question: How is it possible to retrieve last N rows from MySQL database that can be manipulated and be in ASC order ?
You can do it with a sub-query:
SELECT * FROM
(
SELECT * FROM table ORDER BY id DESC LIMIT 50
) AS sub
ORDER BY id ASC;
This will select the last 50 rows from table, and then order them in ascending order.
SELECT * FROM table ORDER BY id DESC LIMIT 50
save resources make one query, there is no need to make nested queries
SELECT * FROM table ORDER BY id DESC, datechat DESC LIMIT 50
If you have a date field that is storing the date (and time) on which the chat was sent or any field that is filled with incrementally (order by DESC) or de-incrementally (order by ASC) data per row put it as second column on which the data should be ordered.
That's what worked for me!!!! Hope it will help!!!!
Use it to retrieve last n rows from mysql
Select * from tbl order by id desc limit 10;
use limit according to N value.
if anyone need this
you can change this into
SELECT
*
FROM
`table`
WHERE
id > ((SELECT
MAX(id)
FROM
chat) - 50)
ORDER BY id ASC;
into
SELECT
*
FROM
`table`
WHERE
id > (SELECT MAX(id)- 50 FROM chat)
ORDER BY id ASC;
select * from Table ORDER BY id LIMIT 30
Notes:
* id should be unique.
* You can control the numbers of rows returned by replacing the 30 in the query
I am not sure if this is possible or not
I have a table with orders and userid
I need to do a SELECT query with ORDER BY the least number of orders of a customer
Example:
userid1 has 10 orders
userid2 has 5 orders
Then SELECT should show userid2 at the top
Thanks in advance
If you have orders as ordersCount i mean as a field which keeps counts of orders then
Select * FROM YourTable order by orders ASC
Else if you have a record for each order then
Select * FROM YourTable Group by UserID order by count(*) ASC;
Apply limit like this
Select *
FROM YourTable
Group by UserID
order by count(*) ASC
limit startIndex, fetchSize;
where startIndex and fetchSize are int that define your limit
You need to group by the userid so you can count orders by user.
SELECT userid, count(1) FROM orders GROUP BY userid ORDER BY count(1) asc
You can accomplish that using GROUP BY and ordering by COUNT().
It would be something like that.
SELECT userid, COUNT(*) qt
FROM orders
GROUP BY userid
ORDER BY qt
If each order is in its own row, however, you need to aggregate them:
SELECT * from MYTABLENAME GROUP BY userid ORDER BY COUNT(*)
Count is what you're looking for
SELECT count(theField) as theCount, *
FROM theTable
GROUP BY theField
ORDER BY theCount
You can select the COUNT of a column and give it a name, then use that name to sort by. Here's an example:
SELECT
p.*,
(SELECT COUNT(o.order_id) FROM orders AS o WHERE p.partron_id=p.patron_id) AS orderCount
FROM
patrons AS p
WHERE
...
ORDER BY
orderCount ASC
UPDATE: Just realized this is only useful if you have orders in a separate table than patrons. Disregard if you are only looking at a single table.
This query expects you to have two tables, a patron table with patron information like name, and a orders table that has a patron_id column that matches the identity column in the patron table.
The advantage to doing this is that you can request patron information at the same time, so you don't need to run two queries in PHP if you're going to use that information.
Is it possible to have something like this in one query,
Count how many likes a certain ID has in image_likes and then order results from images descending by how many likes they have.
SELECT
*
FROM
`images`
ORDER BY
(SELECT COUNT(`id`) FROM `image_likes` WHERE `image_id`=images.`id`) ASC
(I have of course made up field names, but this format should work)
If possible, you might want to change the way the system works so that you can just read the total likes from a field name rather than doing a subselect.
Untested
select imageid, count(imageid) from image_likes
Group by imageid
Order by Count(imageid) desc
select * from (SELECT *,(SELECT COUNT(*) as count from image_likes il WHERE ID = i.ID)
FROM images) tbl ORDER BY COUNT
untested
So basically I'm getting notifications of new content on my website. I have 4 tables -
articles
media
updates
comments
Each table has a set of its own columns (I can include these if anyone wants). There is one distinct column every table has, this is the timestamp column (a big int formatted column with data from the PHP time() function). My solution to getting the last 30 modifications is to select the first 30 rows from these 4 tables ordered by timestamp descending.
Here is the query I have so far, it doesn't work and I'm wondering if someone could help me. -
SELECT * FROM `articles`
UNION SELECT * FROM `media`
UNION SELECT * FROM `updates`
UNION SELECT * FROM `comments`
ORDER BY `timestamp` DESC
LIMIT 30
EDIT:
I was also using another query before -
SELECT * FROM `articles` ,`media` ,`updates` ,`comments`
ORDER BY `timestamp` DESC
LIMIT 30
and kept getting this error -
Column 'timestamp' in order clause is ambiguous
EDIT 2
I realise now I have to use the AS clause in my statement to combine these results into one table.
SELECT a.*,m.*,u.*,c.* from articles AS a
LEFT JOIN media AS m ON (m.timestamp = a.timestamp)
LEFT JOIN updates AS u ON (u.timestamp = a.timestamp)
LEFT JOIN comments AS c ON (c.timestamp = a.timestamp)
ORDER BY timestamp desc LIMIT 30
Your union can work, but only if you can create some sort of common field list. For example, lets say you have a description field in each table, with different names. Something like this will work...
SELECT TimeStamp,'Articles',Art_desc AS Description FROM articles
UNION ALL
SELECT TimeStamp,'Media',Media_Desc FROM Media
UNION ALL
SELECT TimeStamp,'Updates',Update_Desc FROM Updates
UNION ALL
SELECT TimeStamp,'Comments',Comment FROM Comments
ORDER BY timeStamp DESC LIMIT 30
In essence, you are creating result sets of 3 consistent columns, so UNION will work in this case.
I have had a look through google results, but can't find a simple answer, I have a table
id, points (and more fields, but they dont affect anything)
How would I go about Getting position of record 24(id) ordered by points DESC?
I'm not sure I've understood your question
select * from table order by points desc limit 23,1
Select Count(*) from (
Select ID from UnNamedTable A where A.points>(Select B.Points from UnNamedTable B where B.ID=24)
)
Are you trying to figure out out what place a user is in based on a points system?
If so then just find how many users have more points than a particular user then add 1.
SELECT COUNT(*) + 1 AS 'position' FROM table WHERE points >
( SELECT points FROM table WHERE id = someIDhere )
You could create a variable to keep track of your row position after sorting. Something like this would work:
select #rownum:=#rownum+1 ‘rank’, p.* from table1 p, (SELECT #rownum:=0) ID order by Points desc limit 10;