Pulling some coupons from a database. Each coupon has a merchantid column that contains the id for the merchant for which the coupon belongs too.
I'm trying to construct a query that pulls 5 coupons, but I only want 1 coupon per merchantid. I don't want multiple coupons with the same merchantid.
You could use
SELECT * FROM coupons GROUP BY merchantid LIMIT 0,5;
And it will work because
MySQL extends the use of GROUP BY so that the select list can refer to nonaggregated columns not named in the GROUP BY clause (see docs)
If you don't want MySQL to decide which merchantid to keep, you can add your condition
(in example below - keep merchant with highest number of clicks) using subquery:
FIXED:
SELECT c1.*
FROM coupons c1 JOIN (
SELECT t.merchantid, MAX(t.numberofclicks) maxnumberofclicks
FROM coupons t GROUP BY t.merchantid
) c2 ON c1.merchantid = c2.merchantid AND c1.numberofclicks = c2.maxnumberofclicks
LIMIT 0,5;
And one more (more concise and probably faster on large datasets) way to skin a cat:
SELECT c1.*
FROM coupons c1 JOIN coupons c2 ON c1.merchantid = c2.merchantid
GROUP BY c1.merchantid, c1.numberofclicks
HAVING c1.numberofclicks = MAX(c2.numberofclicks)
LIMIT 0,5;
If you want 5 coupons with overall highest number of clicks, add ORDER BY c1.numberofclicks DESC before LIMIT 0,5.
try
SELECT * FROM your_table_name GROUP BY merchantid LIMIT 0,5;
this would give 5 rows which has distinct merchantid's, but you may get the same result for different executions. if you want to randomize it, randomize 'A' inside 'LIMIT A,5'.
Related
Basically, I have two separate queries, which I need to somehow merge into one set of results.
![This is Table 1, which shows the sum of each group's salary]
1
Here is the queries I wrote to form the tables.
SELECT con_stagename, SUM(p_daily_salary) AS sum_salary
FROM CONTENDER, PARTICIPANT
WHERE p_contender = con_id
GROUP BY con_id;
SELECT MAX(sum_salary) AS max_salary
FROM (SELECT con_stagename, SUM(p_daily_salary) AS sum_salary
FROM CONTENDER, PARTICIPANT
WHERE p_contender = con_id
GROUP BY con_id) T2;
And the question is, if I want the result to be a single row of values, which the name of the group with the highest salary, and the actual amount. How would I do it? I've been trying to use JOIN operations but there was not luck.
SELECT con_stagename, SUM(p_daily_salary) AS sum_salary
FROM CONTENDER, PARTICIPANT
WHERE p_contender = con_id
GROUP BY con_id
ORDER BY 2 DESC
LIMIT 1
I'm trying to create query that will show me table of stock, name of the stock, id, date, url, price and list of prices from the last 2 weeks.
For the 14 days history I used sub-query with group_concat on the select.
But when I use group_concat it's return all results and ignore my limit, so I created another sub-query that will be the 14 prices and the group_concat will make it a list.
The table 'record_log' is records for all stocks:
parent_stock_id - the actual stock this line belongs
price - the price
search_date - date of the price
The second table is 'stocks':
id - id of the stock
name, market_volume....
Here is the problem:
In the sub-sub-query (last line of the SELECT), when i'm filtering parent_stock_id=stocks.id he don't recognize the stocks.id because it belongs to the main query.
How can I take the stock_id from top and pass it to the sub-sub-query? or maybe another idea?
SELECT
stocks.id AS stock_id,
record_log.price AS price,
record_log.search_date,
(SELECT GROUP_CONCAT(price) FROM (SELECT price FROM record_log WHERE parent_stock_id=stocks.id ORDER BY id DESC LIMIT 14) AS nevemind) AS history
FROM stocks
INNER JOIN record_log ON stocks.id = record_log.parent_stock_id
WHERE
record_log.another_check !=0
Thank you!
--- I'm are not really using it for stocks, it's just was the easiest way to explain :)
One method is to use substring_index() and eliminate the extra subquery:
SELECT s.id AS stock_id, rl.price AS price, rl.search_date,
(SELECT SUBSTRING_INDEX(GROUP_CONCAT(price ORDER BY id DESC), ',', 14)
FROM record_log rl2
WHERE rl2.parent_stock_id = s.id
) AS history
FROM stocks s INNER JOIN
record_log rl
ON s.id = rl.parent_stock_id
WHERE rl.another_check <> 0;
Note that MySQL has a settable limit on the length of the group_concat() intermediate result (group_concat_max_len). This parameter is defaulted to 1,024.
I have two tables, group and groupAccess. One contains information for each "group", and the other contains all the times a group is accessed, including it's unique ID and timestamp.
My goal is to order all groups by the most recent time they were accessed.
I've got half way there, this query allows me to get all the groups in the correct order, however there are duplicate groups that I need to remove.
SELECT a.*
FROM groups a
INNER JOIN groupAccess b ON a.group_id = b.group_access_id
ORDER BY access_time DESC
I've tried using GROUP BY or DISTINCT, however this breaks the (currently) correct order of the groups. How can I fix this?
Use max() and GROUP BY to find the latest access time of each group, and then join that table with group. That is:
SELECT
a.*
, b.last_access_time
FROM
groups a
INNER JOIN (
select
group_access_id
, max(access_time) as last_access_time
from groupAccess
group by
group_access_id
) b ON a.group_id = b.group_access_id
ORDER BY b.last_access_time DESC
I'd like to order contents of a table from MySQL database that looks like below.
name,h1,h2
a,f1,3
a,g3,5
a,h3,4
b,g3,4
c,h5,2
c,j12,6
I'd like to get the lengths for each element in name column i.e, length of 'a' would be 3 (since it has three rows of data associated with it) and get data of the top 2 elements (here it'd include 3 rows for a's and 2 for c's since they have the highest length in descending order). So the required output would look something like below
name,h1,h2
a,f1,3
a,g3,5
a,h3,4
c,h5,2
c,j12,6
How can this be achieved in MySQL?
Joining to a count of the top two records should give you your results.
select
t.*
from
table1 t
inner join (select
name,
count(*) as cnt
from table1
group by name
order by count(*) desc
limit 2) as c on t.name = c.name
order by c.cnt desc
Here's a fiddle
I am trying to get a list of blog_id and the sum of donations on that blog id from a table called donations. And I want to order it by the sum of donations. Basically I want to produce a list of blogs ranked by donations. Blogs are held in a different table referenced by blog_id.
This is what I have been trying but all it does is sum up all donations and produce 1 row. I don't understand what I did wrong here!
$donations_result = mysql_query("SELECT blog_id, sum(amount) FROM donations ORDER BY sum(amount)");
Donations table is a series of blog_ids and individual donations. So something like this:
blog_id--donation
1 ----------26
1 ----------1
2 ----------24
2 ----------12
You did not group the columns. You need to group it by non-aggregated column and in this case by blog_id
SELECT blog_id, sum(amount) TotalSum
FROM donations
GROUP BY blog_id
ORDER BY TotalSum
The reason why your query executed well without throwing an exception is because mysql permits to use aggregate function without specifying non-aggregated column in the GROUP BY clause.
see MySQL Extensions to GROUP BY
You're missing the GROUP BY operator:
SELECT blog_id, sum(amount)
FROM donations
GROUP BY blog_id
ORDER BY sum(amount)