[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

sql statement mysql

Posted on 2011-04-25
2
Medium Priority
?
233 Views
Last Modified: 2012-05-11
what wrong with code below when i fill (select  amphur.AMPHUR_NAME where c = SUBSTRING(m.own,3,4)  ) it error

CREATE TABLE IF NOT EXISTS `member` (
  `idmember` int(11) NOT NULL AUTO_INCREMENT,
  `own` varchar(255) DEFAULT NULL,
  `Sname` varchar(255) DEFAULT NULL,
  `Ssurname` varchar(255) DEFAULT NULL,
  `Sex` enum('¿','¿') NOT NULL,
  `Hno` varchar(255) DEFAULT NULL,
  `Moo` varchar(255) DEFAULT NULL,
  `tambol` varchar(200) NOT NULL,
  `dateofbirth` date DEFAULT NULL,
  `migratedate` date DEFAULT NULL,
  `status` enum('5','4','3','2','1') DEFAULT '5',
  `Unit` int(4) DEFAULT NULL,
  `staff1` int(11) DEFAULT NULL,
  `staff2` int(11) DEFAULT NULL,
  `fathercode` varchar(30) NOT NULL,
  `mathercode` varchar(30) NOT NULL,
  PRIMARY KEY (`idmember`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=8994 ;


CREATE TABLE IF NOT EXISTS `amphur` (
  `AMPHUR_ID` int(5) NOT NULL AUTO_INCREMENT,
  `AMPHUR_CODE` varchar(4) COLLATE utf8_unicode_ci NOT NULL,
  `AMPHUR_NAME` varchar(150) COLLATE utf8_unicode_ci NOT NULL,
  `GEO_ID` int(5) NOT NULL DEFAULT '0',
  `PROVINCE_ID` int(5) NOT NULL DEFAULT '0',
  `province_name` varchar(80) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`AMPHUR_ID`),
  KEY `province_name` (`province_name`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=999 ;
SELECT SUBSTRING(m.own,3,4) as c , (select  amphur.AMPHUR_NAME where c = SUBSTRING(m.own,3,4)  ),
         COUNT(* ) AS cnt 
    FROM MEMBER AS m 
         
   GROUP BY SUBSTRING(m.own,3,4)
order by cnt desc

Open in new window

0
Comment
Question by:teera
2 Comments
 
LVL 39

Expert Comment

by:Aaron Tomosky
ID: 35460382
select  amphur.AMPHUR_NAME from amphur
0
 
LVL 41

Accepted Solution

by:
ralmada earned 2000 total points
ID: 35460435
try

SELECT SUBSTRING(m.own,3,4) as c ,
      (select max(amphur.AMPHUR_NAME) from amphur),
         COUNT(* ) AS cnt
FROM MEMBER AS m
GROUP BY SUBSTRING(m.own,3,4)
order by cnt desc

Now, I'm not sure if you have any field in common between both tables, in that case I think this is what you're looking for:

SELECT SUBSTRING(m.own,3,4) as c ,
      a.Amphur_name,
         COUNT(* ) AS cnt
FROM MEMBER AS m
inner join amphur a on m.yourfieldincommon = a.yourfieldincommon
GROUP BY SUBSTRING(m.own,3,4), a.amphur_name
order by cnt desc
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question