This question already has answers here:
How to use count and group by at the same select statement
(11 answers)
Closed 6 years ago.
SELECT exam_board, COUNT(*)
FROM subjects
GROUP BY exam_board;
This is a block of code where 'exam_board' is a field in a table called 'subjects'.
What does each line of code in this block do?
Gives you how much records have the same exam_board value for each different exam_board value.
For example, if your table has this data:
|exam_board|
A
A
A
B
B
this query will return:
|exam_board |COUNT(*)
A 3
B 2
Gives you the count of total number of records fetched in query.
In your specific case, it will give you count of each exam_board group.
exam_board count
A 10
B 29
Something like this.
Related
This question already has answers here:
Retrieving the last record in each group - MySQL
(33 answers)
Closed last month.
I want to write a mysql query that selects the final result in the figure below :
Thanks
Any solution, for this question.
This may help you, Execute query as below:
SELECT *
FROM tickets
GROUP BY device_id
HAVING key_field = MAX(key_field)
ORDER BY device_id ASC
Will produce result as you expected:
Key
ID device
ticket Number
212
1
594
211
2
147
This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 2 years ago.
Assuming a table Family with Name and Age and records
Bob 55
Alice 40
Marky 12
If I run
Select Name,Min(Age) from Family
I get
Bob,12
I'm trying to ask for the fields from the single record with the lowest age yet I'm simply getting the first record Name and the Age from the record with the lowest value.
How can I use Min() to make that request?
I understand that you want the entire row that has the smallest age. Assuming that you don't care about ties, you can just sort the rows by ascending age and retain the first record only:
select *
from family
order by age
limit 1
This question already has answers here:
Select row with most recent date per user
(14 answers)
Closed 4 years ago.
I'm trying to come up with a single mySQL query that will take the data below and output the sample output I provided.
Basically what I'm looking for is an output that show only the most recent entry for each person in the table, (their name and the total_points).
The catch here is I only want to show people who have at least 200 points in their most recent entry. In my example output only jeff and bob would have at least 200 points , but ted would NOT and should not be part on the output.
Is there a way to do this in a single query or would I have to break it out into separate queries?
Table Structure:
person------------date--------------------------------------total_points
jeff-----------------2018-07-16 09:00:00----------------300
bob----------------2018-07-15 09:00:00----------------500
ted-----------------2018-07-09 09:00:00----------------100
jeff-----------------2018-07-09 09:00:00----------------700
bob----------------2018-07-03 09:00:00----------------180
ted-----------------2018-06-10 09:00:00----------------1200
Output:
person--------total_points
jeff-------------300
bob------------500
One method uses a correlated subquery to get the most recent value:
select t.*
from t
where t.date = (select max(t2.date) from t t2 where t2.person = t.person) and
t.total_points >= 200;
This question already has answers here:
Find total number of results in mySQL query with offset+limit
(7 answers)
Closed 6 years ago.
I am making a pagination function, here is my case:
1 table (example)
id | title | date | details
Now, I want to retrieve two different results from this table (example)
Count all of the rows (for the total count of all the lists)
I will only show every 10 list per page.
My current code is, I have 2 separated queries for 1 and 2, so it is like 2 connections, my question is, can this be done with a single query and then retrieve both of 1 and 2 results? If so, what do I need to do? Any suggestion/s can help me!
I think,
This will help you.
Step 1: Get the all list from the table
Step 2: Then count the records
Here is the single query to perform it.
SELECT COUNT(tmp.id) as cnt, tmp.* FROM (SELECT id, title, date, details FROM tablename) tmp
This question already has answers here:
MYSQL REGEXP search in JSON string
(3 answers)
Closed 7 years ago.
I have three records in mysql:
1. a:7:{s:10:"state_name";s:11:"West Bengal";s:7:"city_id";a:40:{i:0;s:4:"1166";i:1;s:4:"1454";i:2;s:4:"1455";i:3;s:4:"1456";i:4;s:4:"1458";i:5;s:4:"1459";i:6;s:4:"1460";i:7;s:4:"1461";i:8;s:4:"1463";i:9;s:4:"1464";i:10;s:4:"1465";i:11;s:4:"1466";i:12;s:4:"1468";i:13;s:4:"1469";i:14;s:4:"1470";i:15;s:4:"1471";i:16;s:4:"1473";i:17;s:4:"1474";i:18;s:4:"1475";i:19;s:4:"1476";i:20;s:4:"1478";i:21;s:4:"1479";i:22;s:4:"1480";i:23;s:4:"1481";i:24;s:4:"1483";i:25;s:4:"1484";i:26;s:4:"1485";i:27;s:4:"1486";i:28;s:4:"1488";i:29;s:4:"1489";i:30;s:4:"1490";i:31;s:4:"1491";i:32;s:4:"1493";i:33;s:4:"1494";i:34;s:4:"1495";i:35;s:4:"1496";i:36;s:4:"1498";i:37;s:4:"1499";i:38;s:4:"1500";i:39;s:4:"1501";}s:6:"gender";s:4:"male";s:3:"age";s:0:"";s:12:"category_ids";s:2:"68";s:16:"product_type_ids";s:2:"30";s:18:"min_purchase_price";s:1:"0";}
2. a:7:{s:10:"state_name";s:11:"West Bengal";s:7:"city_id";a:8:{i:0;s:4:"1465";i:1;s:4:"1466";i:2;s:4:"1467";i:3;s:4:"1471";i:4;s:4:"1475";i:5;s:4:"1476";i:6;s:4:"1478";i:7;s:4:"1479";}s:6:"gender";s:0:"";s:3:"age";s:4:"0-30";s:12:"category_ids";s:2:"58";s:16:"product_type_ids";s:0:"";s:18:"min_purchase_price";s:2:"50";}
3. a:7:{s:10:"state_name";s:11:"West Bengal";s:7:"city_id";a:1:{i:0;s:4:"1475";}s:6:"gender";s:6:"female";s:3:"age";s:4:"0-30";s:12:"category_ids";s:2:"58";s:16:"product_type_ids";s:0:"";s:18:"min_purchase_price";s:3:"100";}
with the column name conditions_serialized.
Now I am trying to write a sql query to fetch all the records having city_id 1475.
My code is like :
SELECT `main_table`.*, `rule_coupons`.`code` FROM `salesrule` AS `main_table` LEFT JOIN `salesrule_coupon` AS `rule_coupons` ON main_table.rule_id = rule_coupons.rule_id AND rule_coupons.is_primary = 1 WHERE (conditions_serialized regexp 'city_id";a:[0-9]*:{.*(i:[0-9]*;s:[0-9]*:"1475";)}')
But by this only 2 records are displaying 2nd and 3rd, not the first one.
Could you please check my query and rectify that why it is not displaying all?
Thanks in advance.
You are looking explicitly for 1475
city_id";a:[0-9]*:{.*(i:[0-9]*;s:[0-9]*:"**1475**";)}
change it to this instead to get records from the 3 records. Note: from 3 to 5 digits
city_id";a:[0-9]*:{.*(i:[0-9]*;s:[0-9]*:"\d{3,5}";)}