Multiple selection of ID's (MySQL) - mysql

Is it possible for me to select multiple ID? For example: I am executing this command
SELECT
sum(amount)
FROM
bets_logs
where
fight_id=1
Assumming that I have id that is up to 500 Of course I'll do it one by one... My target is, is there a query that I can select id from 1 to 10. So that I won't do it 1 by one.

My target is, is there a query that I can select id from 1 to 10. So
that I won't do it 1 by one.
You can write the query like following.
SELECT fight_id, sum(amount)
FROM
bets_logs
where
fight_id >=1 and fight_id<=10
group by fight_id

Related

find first row count and second row count using mysql

I have a table in mySql. I need to find how much entry in table which have entered only one time and another records which are enter for second time. please see the screenshot. count is based on shg_id.
if I correctly understand, you need this:
select entered, count(*) from (
select shg_id, count(*) as entered
FROM mytable
group by shg_id
having count(*) between 1 and 2
)t
group by entered
The following should do if it has an id attribute:
SELECT * FROM Table
HAVING COUNT(shg_id) = 1 -- Record equal to 1
Or
SELECT * FROM Table
HAVING COUNT(shg_id) = 2 -- Record equal to 2
Updated - This works well on my side:
SELECT COUNT(shg_id) AS Total
FROM Table
WHERE shg_id= 4
GROUP BY shg_idHAVING COUNT(shg_id) = 1
Another one - Slightly taken from OTARIKI:
SELECT shg_id, COUNT(*) AS Total FROM Table
GROUP BY shg_id
HAVING COUNT(shg_id) BETWEEN 1 and 2

MySQL count select query with condition

I have a mysql table called Game which has two columns, Name and Score. I want to select only the Names whose scores have been atleast 100 and atleast twice. In the below example Ron and Mary will get selected. I am not sure how to write the select statement for this.
Game table
Use GROUP BY with a HAVING clause:
SELECT Name
FROM mytable
GROUP BY Name
HAVING COUNT(CASE WHEN Score >= 100 THEN 1 END) >= 2
HAVING clause checks for Name groups, having at least two records with Score >= 100.

How can i apply Multiple In clause in a single MySQL query

I have list of ids and corresponding creation dates
for Exmple :
1 2014-05-01
2 2014-07-01
3 2014-08-01
Need suggestion regarding writing a MySQL select statement which gives id details after corresponding creation date.
select id,count(*) from id_details where id IN(1,2,3) where resolved_at >(2014-05-01,2014-07-01,2014-08-01) group by id
The date condition for resolved_at column is not correct. Again, if you have two WHERE clause in your query, that as well not correct. You can's specify > condition in IN clause like you are trying. Your query should look like
select id,count(*)
from id_details
where id IN (1,2,3)
and (resolved_at >= '2014-05-01'
and resolved_at <= '2014-08-01')
group by id
I think you are just trying to use IN operator for resolved_at column like
select id,
count(*)
from id_details
where id IN (1,2,3)
and resolved_at IN ('2014-05-01','2014-07-01','2014-08-01')
group by id

Sorting results of UNION query combining two tables by the table of origin

I'm using the UNION operator to select results from two different tables. I want results from the first table result to come before those from the second table.
For example: I have the tables customer_coupons and segment_coupons. Both tables have a column named coupon_id. When I run a query involving a UNION of these two tables, it returns the correct records, but they are not the order I want: It gives me the coupon_ids of both tables mixed in ascending order, but I want to show ALL coupon_ids of the first table and then ALL coupon_ids of the second table.
Here's the query as it currently exists:
SELECT coupon_id
FROM customer_coupons
UNION
SELECT coupon_id
FROM segment_coupons;
How can I change this so that all results from the first half of the query come before all results of the second half?
Put in a fixed table-identifying field:
(SELECT 1 AS source_table, coupon_id
FROM customer_coupons)
UNION ALL
(SELECT 2 AS sourcE_table, coupon_id
FROM segment_coupons)
ORDER BY source_table, coupon_id
Note the brackets around the individual queries. This forces MySQL to apply the order by to the result of the union, not to the 2 sub-query.
SELECT * FROM (
SELECT coupon_id, 1 as myorder
FROM customer_coupons
UNION
SELECT coupon_id 2 as myorder
FROM segment_coupons)
Order by myorder

Condition for counting distinct rows in an SQL query

I have a table in a MySQL database with an ID column. This is not a key of the table and several rows can have the same ID.
I don't really know SQL but I already figured out how to obtain the number of distinct IDs:
SELECT COUNT(DISTINCT ID) FROM mytable;
Now I want to count only those IDs which appear more than 2 times in the table.
So if the ID column contains the values
3 4 4 5 5 5 6 7 7 7
the query should return 2.
I have no idea how to do this. I hope someone can help me!
Btw, my table contains a huge number of rows. So if there are several possibilities I would also be happy to know which solution is the most efficient.
Try this:
SELECT COUNT(ID) FROM (
SELECT ID FROM mytable
GROUP BY ID
HAVING COUNT(ID) > 2) p
select count(*) from
(select count(id) as cnt,id from mytable group by id) da
where da.cnt>2
The inner query will give you how many elements does each id have. And the outer query will filter this.
SELECT
COUNT(ids)
FROM
(SELECT
COUNT(ID)AS ids
FROM
mytable
GROUP BY
ID
HAVING
ids>2
)AS tbl1
Updated :
SELECT count(ID)
FROM (
SELECT ID FROM mytable
GROUP BY ID
HAVING count(ID) > 2
) p
should do what you need