Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Trying to get all rows where userid = 'me'
Then 2 newest rows where userid <> 'me'
Items:
Userid time
other2 11
other3 10
me 10
me 8
other1 8
other3 7
me 6
would return
Userid time
me 10
me 8
me 6
other2 11
other3 10
The results don't have to be in any order
(SELECT * FROM Items WHERE userid='me' )
UNION ALL
(SELECT * FROM Items WHERE userid<>'me'
ORDER BY time DESC LIMIT 2)
This only outputs 2 rows
The sql was correct as shown above
(SELECT * FROM Items WHERE userid='me' )
UNION ALL
(SELECT * FROM Items WHERE userid<>'me'
ORDER BY time DESC LIMIT 2)
Union All concatenates the two queries, and the parenthesis allow different WHERE/ORDER BY/LIMIT clauses.
Demonstrated here:
http://sqlfiddle.com/#!9/ca405/1/0
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
So, i have a table with records like this:
ID ORDERS
1 4
1 1
2 4
3 1
3 2
I need to create a query that will lead to this situation:
ID ORDERS
1 5
2 4
I tried with select id, count(orders) from table group by id having count(orders) > 3
but all I get is a strange result in which my ids are strangely numbered.
You need to sum and not count:
select id, sum(orders)
from table
group by id
having sum(orders) > 3
MySQL supports summing up values via sum like follows:
select id, sum(orders) from test group by id;
See also DB Fiddle.
Change your attempt so that you SUM(orders) not COUNT(orders)
Count doesn't work because it increments a counter by one for any non null value, and doesn't increment for a null value. In the the following table:
ID, name
1, John
2, null
3, Jane
null, null
COUNT(id) will return 3 - there are 3 non null ID values. COUNT(name) will return 2 - there are two non null values. SUM(ID) will return 6 - the sum of the numeric values of ID is 6. SUM looks at what the values are, COUNT doesn't. SUM will treat nulls as 0. The special case COUNT(*) will count the presence of the row even if all its values are null. COUNT(*) will return 4 - there are 4 rows in the results
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
$SQL = "SELECT pumpSales._end_date, SUM(pumpSales.AMOUNT) as
totalAmount, payments_data.USER
FROM
payments_data INNER JOIN pumpSales ON payments_data.Pay_sale_id =
pumpSales._sale_id
WHERE
pumpSales._end_date =CURDATE();
GROUP BY
payments_data.USER
ORDER BY
pumpSales._end_date ASC LIMIT 3";
The result I want is
The users with their individual total amounts
Starting with the highest for the day
But
What I get is the name of one user with total amount for all the users
e.g
2018-01-11 user 1 400,000
where 400,000 is the total for user1, user2, user3
You don't have USER in the ORDER BY clauses, and you don't have _end_date in the GROUP BY clause. Consider:
SELECT pumpSales._end_date,
SUM(pumpSales.AMOUNT) as totalAmount,
payments_data.USER
FROM
payments_data
INNER JOIN pumpSales
ON payments_data.Pay_sale_id = pumpSales._sale_id
WHERE
pumpSales._end_date =CURDATE();
GROUP BY
payments_data.USER, pumpsales._end_date
ORDER BY
payments_data.USER, pumpSales._end_date";
It is also not clear what the LIMIT in your query is intended to do.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm working on an IPN for PayPal, and I need to know how to select a certain amount of rows where sold='false'
The following should work:
SELECT * FROM your_table WHERE sold='false' LIMIT 10
select * from table where sold='false' limit 10
OR
select * from table where sold='false' limit M,N
this query fetches data starts from Mth row to N number of records
.
select top 10 * from table where sold='false'
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to select 4 random rows from a table where level = 1 and 4 random rows from the same table where level=2. How can I do this in 1 query ?
select * from (select * from your_table
where level = 1 order by rand() limit 4) x
union all
select * from (select * from your_table
where level = 2 order by rand() limit 4) y
try like this...
SELECT product_id, title, description FROM products WHERE active = 1 AND stock > 0 ORDER BY RAND() LIMIT 4;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have an aggregated table like:
No Field1 Type
12 -1 FR2
12 45 FR1
11 -1 FR2
11 30 FR2
11 20 FR1
Is it possible to select Nos according to types where they possess negative values in Field1 and no other entries with positive values?
Is it possible to find the negation of that selection?
Hence the output should be:
No
11
Explanation:
I am trying to check for people who have failed a list of tests and have not retaken and passed them. Those who never failed as well as those who retook and passed is the result.
In the result output, 12 is not returned because it has negative values for FR2 and no subsequent positive values for FR2.
Thanks.
SELECT No
FROM
( SELECT No, MAX(Field1) AS MaxField1
FROM tableX
GROUP BY No, Type
) AS tmp
GROUP BY No
HAVING MIN(MaxField1) > 0 ;
Tested at SQL-Fiddle
select distinct No from yourtable
group by No,Type
having max(Field1) >0
and min(Field1)<0