MySql: Fetch LIMITED results using GROUP BY clause on one table/view - mysql

I have a view 'view_type' like:
type------name------fid
type_a----name1-----12
type_a----name2-----27
type_a----name3-----45
type_a----name4-----43
type_a----name5-----25
type_a----name7-----75
type_a----name6-----15
type_b----bame1-----12
type_b----bame2-----27
type_b----bame3-----45
type_b----bame4-----43
type_b----bame5-----25
type_c----came7-----55
type_c----came6-----25
Now i want to fetch results having 'ame' in name field, but only from 'type_a' & 'type_b' and also only 4results from each.
type------name------fid
type_a----name1-----12
type_a----name2-----27
type_a----name3-----45
type_a----name4-----43
type_b----bame1-----12
type_b----bame2-----27
type_b----bame3-----45
type_b----bame4-----43
In simple words I want to limit results of 'group by' result set.
Don't want to use 'complex sub query' OR 'stored procedure'. Is there any simple query which help me out?

Try this query
select * from view_type where type = 'type_a' and name regexp 'ame' LIMIT 4
UNION
select * from view_type where type = 'type_b' and name regexp 'ame' LIMIT 4

Related

Grouping mysql by a regex?

I am trying to find the count of how many times a location is used in my table of search results, but I want to group together cases where the same post code area start is used. The following groups purely on the text used:
SELECT count(*),
search_browse_log.postcode_start
FROM search_browse_log
GROUP BY postcode_start
ORDER BY count(*) DESC
But in the data, I have for example CR0, CR1, CR2 (postcode starts). I want to group them all together so I have a count of 3 for "CR", rather than 1 each of CR0, CR1 and CR2.
Thanks in advance if you can help!
Use a conditional in the GROUP BY clause to get either 1 or 2 characters, depending on whether the postcode starts with 1 or 2 letters.
GROUP BY IF(postcode_start REGEXP '^[A-Z][A-Z]',
LEFT(postcode_start, 2),
LEFT(postcode_start, 1))
If it's only the first 2 characters of the postcode, you could use the LEFT function in the group by:
SELECT count(*),
LEFT(postcode_start,2) as `postcode_start`
FROM search_browse_log
GROUP BY LEFT(postcode_start,2)
ORDER BY count() DESC
To group by the non-numeric characters at the start of the string:
SELECT count(*),
IF(postcode_start REGEX '^[a-ZA-Z][0-9]',LEFT(postcode_start,1),LEFT(postcode_start,2)) as `postcode_start`
FROM search_browse_log
GROUP BY IF(postcode_start REGEX '^[a-ZA-Z][0-9]',LEFT(postcode_start,1),LEFT(postcode_start,2))
ORDER BY count() DESC
For reference, see https://dev.mysql.com/doc/refman/5.7/en/regexp.html#operator_regexp

What will be Mysql query for group by and order by dictionary style?

Trying to fetch data and print as like dictionary.
Table:
blog_tags
id name
1 atag1
2 atag2
3 dtag1
4 etag1
5 etag2
6 ctag1
7 ctag2
8 ctag3
9 ztag1
I want the data output as:
A
atag1
atag2
C
ctag1
ctag2
D
dtag1
E
etag1
etag2
Z
ztag1
Started with this:
select name from blog_tags order by name;
what will be mysql query for this?
Try something like this
select name
from (
select distinct upper(substring(name, 1, 1)) as name
from blog_tags
union all
select name
from blog_tags
)
order by name
Edit
If you want to get raw data for application level manipulation, I would suggest querying the db this way
select upper(substring(name, 1, 1)) as key,
name
from blog_tags
order by 1, 2
You can use the bellow query to achieve this as given below
select substr(name,1,1),group_conact(name) from blog_tags group by substr(name,1,1);
This query will group the name's by first character and will group concat the name's as comma separated. You can convert the result from your programming language to array
The output will be like given below
substr(name,1,1) group_conact(name)
A atag1,atag2
C ctag1,ctag2

Select Max and Select other column

I am trying to get the max + 1 value from one column, and all of the values from another column. However, my query does not give any results.
For example,
SectionItemID SectionItem
1 blue
2 red
The query should return
SectionItemID SectionItem
3 blue
red
Heres what I have
SELECT SectionItem,MAX(SectionItemID) + 1 AS SectionItemID FROM Core.SectionItem_Lkup
SELECT SectionItem,
(select MAX(SectionItemID)+1 FROM Core.SectionItem_Lkup) AS SectionItemID
FROM Core.SectionItem_Lkup
Whenever you GROUP BY, you should aggregate the other columns involved.
Mysql does allow to omit aggregation on other colums
MsSQL does not, cause the result is undefined for columns without Aggregation.
Best way is to aggregate other columns. For your szenario, you could use group_concat
SELECT MAX(SectionItemID)+1, Group_concat(SectionItem) FROM tbl
Note: The query does not contain any Group By, because you dont want to group on SectionItemId nor SectionItem. Omiting the Group By and using aggregate-functions will use them on the whole table.
Output:
MAX(SECTIONITEMID)+1 GROUP_CONCAT(SECTIONITEM)
3 blue,red
http://sqlfiddle.com/#!2/353bf3/6
select case when t2.SectionItem = 'blue'
then cast(max(t1.SectionItemID) + 1 as varchar(1))
else '' end
as SectionItemID
, t2.SectionItem
from Core.SectionItem_Lkup t1
full outer join Core.SectionItem_Lkup t2 on 1 = 1
group by t2.SectionItem
order by
case when t2.SectionItem = 'blue'
then cast(max(t1.SectionItemID) + 1 as varchar(1))
else '' end
desc

MYSQL retrieve data dependent on rows returned

I am working on a mysql query that will filter out certain occurrences dependent on how many rows are returned.
I am trying to filter out any support categories when the number of rows returned are 1, however leave the support category in when the result set turned is more than 1.
I originally had this idea however it seems as if it will not work.
SELECT stockmaster.description, SUM(salesorderdetails.quantity), stockmaster.categoryid as qty
FROM salesorderdetails, stockmaster
where salesorderdetails.stkcode=stockmaster.stockid
and orderno='5222'
group by stockmaster.description
HAVING CASE WHEN stockmaster.categoryid = 'S&M' THEN COUNT(*) >= 2 ELSE COUNT(*) = 1 END
Any help will be gratefully accepted.
Try this
SELECT *
FROM
(
SELECT stockmaster.description,
SUM(salesorderdetails.quantity),
stockmaster.categoryid as qty ,
COUNT(*) AS count
FROM salesorderdetails, stockmaster
where salesorderdetails.stkcode=stockmaster.stockid
and orderno='5222'
group by stockmaster.description
HAVING CASE WHEN stockmaster.categoryid = 'S&M'
) MAIN_DATA
WHERE MAIN_DATA.count >1

How to make MySQL result set the same as specified?

Select * from table where id in ($id_list)...
How to make the result the same order as specified in $id_list ?
If $id_list='2,1,5...' then the result set should be in the same order
Try FIND_IN_SET
SELECT * FROM tablename
WHERE id IN (1,5,10)
ORDER BY FIND_IN_SET(id, '1,5,10')
you need to order by with switch case
SELECT * FROM table
WHERE id IN (3,6,1,8,9)
ORDER BY CASE id WHEN 3 THEN 1
WHEN 6 THEN 2
WHEN 1 THEN 3
WHEN 8 THEN 4
WHEN 9 THEN 5
END
read more details on
MySQL ORDER problem
Use the FIELD() function.
SELECT *
FROM table
WHERE id IN ($id_list)
ORDER BY FIELD(id, $id_list)
Using FIELD() is better then using FIND_IN_SET(), Since they are compared as numbers.