I have a table with multiple columns ordered by one attribute (Attr1) and I would like to select (with mySQL) always the first row of a new value:
How do I do this?
Thanks a lot!
EDIT:
Sorry, for not stating my question clear enough.
Consider this new example table:
Let's say I run a blog with several posts which can can be commented by users. On the first page (the starting page) I want to list the newest comment for each post. I therefore get all comments and order them first by post_id, then by time stamp (ORDER BY post_id, time stamp DESC).
However, as I said, I'm only interested in the newest comment (for each post), not all of them. The desired output is therefore the orange rows as it is the newest comment for post_id 1, post_id 2 and post_id 3.
If I solved this with PHP I would simply take the whole table and loop through all rows and only echo the comment if the post_id doesn't match with the previous one.
Hope that makes it clearer. Is this possible with MySQL?
Use Limit and order by clause
select * from your_table ORDER BY Attr1 desc Limit 1
If you want always the lowest ID as the first result AND you want all those lines which are orange, this would work for you!
SELECT * FROM your_table GROUPY BY Attr1 ORDER BY id
But as another answer said, he doesnt think that you want all those lines but instead only one then use his answer..
Database table:
Result:
Related
I am trying to do a simple test where I'm pulling from a table the information of a specific part number as such:
SELECT *
FROM table_name
WHERE part_no IN ('abc123')
This returns 25 rows. Now I want to count the number that meet the "accepted" condition in a specific column but the result is limited to only the 10 most recent. My approach is to write it as follows:
Select Count(*)
FROM table_name
WHERE part_no IN ('abc123') AND lot IN ('accepted')
ORDER BY date DESC
LIMIT 10
I'm having a hard time to get the ORDER BY and LIMIT operations to work. I could use help just getting it to limit appropriately, and I can figure out the rest from there.
Edit: I understand that the operations are happening on the COUNT which only returns one row with a value; but I put the second clip to show where I am stuck in my thought process.
Your query SELECT Count(*) FROM ... will always return exactly one row.
It's not 100% clear what exactly you want to do, but if you want to know how many of the last 10 have been accepted, you could use a subquery - something like:
SELECT COUNT(*) FROM (
SELECT lot
FROM table_name
WHERE part_no IN ('abc123')
ORDER BY date DESC
LIMIT 10
)
WHERE lot IN ('accepted')
The inner query will return the 10 most recent rows for part abc123, then the outer query will count the accepted ones.
There are also other solution (for example, you could have the inner query output a field that is 0 when the part is not accepted and 1 when the part is accepted, then take the sum). Depending on which exact dialect/database you are using, you may also have more elegant options.
Select count returns ONE ROW therefore the ORDER BY and the LIMIT will not work on the results
I hope someone could give me a general direction on this problem:
The starting point is an array of ids of db records.
array ids = [45,23,14,7];
Those records have some columns, i.e.
id,price,rating
7,$5.00,5
14,$2.00,4
23,$5.00,2
45,$5.00,5
What I would need is
the items with max(price) (or something equivalent).
if there is more than one item with the same price, get the ones with max(rating) (or something equivalent).
Finally, if there is still more than one item, take the one that comes first in the array.
I'm particularly stuck with point 3. Is there a way to do that in (My)SQL, or should I do that in code?
Thank you for your reading.
Something like this should work:
SELECT * FROM table WHERE id IN (45,23,14,7) ORDER BY price DESC, rating DESC LIMIT 1
In addition to the answer by #jasonlfunk you can add an extra order clause to take into account your array as well:
SELECT * FROM table WHERE id IN (45,23,14,7) ORDER BY price DESC, rating DESC, FIELD(id,45,23,14,7) ASC LIMIT 1
...I think about your point 3 ..it must be done in code, the result of mysql not necessarily returns results in the order or the array, if applying order by price, then by rating still returning more than one item your code should be able to receive a list instead a single row, and then make the comparisson in code.
I have the following SQL query , it seems to run ok , but i am concerned as my site grows it may not perform as expected ,I would like some feeback as to how effective and efficient this query really is:
select * from articles where category_id=XX AND city_id=XXX GROUP BY user_id ORDER BY created_date DESC LIMIT 10;
Basically what i am trying to achieve - is to get the newest articles by created_date limited to 10 , articles must only be selected if the following criteria are met :
City ID must equal the given value
Category ID must equal the given value
Only one article per user must be returned
Articles must be sorted by date and only the top 10 latest articles must be returned
You've got a GROUP BY clause which only contains one column, but you are pulling all the columns there are without aggregating them. Do you realise that the values returned for the columns not specified in GROUP BY and not aggregated are not guaranteed?
You are also referencing such a column in the ORDER BY clause. Since the values of that column aren't guaranteed, you have no guarantee what rows are going to be returned with subsequent invocations of this script even in the absence of changes to the underlying table.
So, I would at least change the ORDER BY clause to something like this:
ORDER BY MAX(created_date)
or this:
ORDER BY MIN(created_date)
some potential improvements (for best query performance):
make sure you have an index on all columns you querynote: check if you really need an index on all columns because this has a negative performance when the BD has to build the index. -> for more details take a look here: http://dev.mysql.com/doc/refman/5.1/en/optimization-indexes.html
SELECT * would select all columns of the table. SELECT only the ones you really require...
Here is my case, I have a database table with below fields:
name
place_code
email
phone
address
details
estd
others
and example data
If you look at the above example table, first three records are talking about xyz and place code 1020.
I want to create a single record for these three records based on
substring(name,1,4)
place_code
(I am lucky here for all the similar records satisfies this condition and unique in the table.)
For the other columns which record column length has max. For example again for the above 3 records email should be test#te.com, phone should be 657890 and details should be "testdetails".
This should be done for all the table. (Some has single records and some has max 10 records.)
Any help on query that helps me to get the desired result?
Answer
Some one posted the below answer and deleted it . But that looks a good solution
SELECT max(name),
place_code,
max(email),
max(phone),
max(address),
max(details),
max(estd),
max(others)
FROM table_x
GROUP BY substring(name,1,4),place_code
Please let me know if you guys see any issues in it ?
Thank You all
Kiran
You need the awesome GROUP_CONCAT aggregate function.
SELECT place_code,
substring(name,1,4) name,
GROUP_CONCAT(email),
GROUP_CONCAT(Phone),
GROUP_CONCAT(details)
FROM table
GROUP BY place_code, substring(name,1,4)
It has options allowing you to control things like the order of items in the string and the separators. See http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
SELECT max(name),
place_code,
max(email),
max(phone),
max(address),
max(details),
max(estd),
max(others)
FROM table_x
GROUP BY substring(name,1,4),place_code
Hi i have a columm in the table and i want to select the most common item from the selected column. The table is set up
publication:
id
Title
published
I want to beable to select the most recurring places where publications have been published. Is this possible to do?
Thanks in Advance
Dean
select published, count(*) nbr
from table1
group by published
order by nbr desc
limit 1
You don't really need the count, but if you wanted confirmation that the choice seemed reasonable, you could use it. Also, you didn't specifically say whether you wanted ONLY the one, or wanted to see which was the most frequent, along with frequencies of the other records. Take off the limit 1 if you want to see all records.