SQL UNION with Where clause a result of the first select - mysql

I need to execute the following query:
SELECT (COL1,COL2, ClientID)
FROM Jobs
Union
SELECT (ClientID,COL2,COL3)
FROM Clients WHERE (the ClientID= ClientID my first select)
I'm really stuck, I've been trying joins and unions and have no idea how to do this.
*EDIT*Query to create jobs table
CREATE TABLE IF NOT EXISTS `jobs` (
`JobID` int(11) NOT NULL AUTO_INCREMENT,
`Title` varchar(32) NOT NULL,
`Trade` varchar(32) NOT NULL,
`SubTrade` varchar(300) NOT NULL,
`Urgency` tinyint(4) NOT NULL,
`DatePosted` int(11) NOT NULL,
`Description` varchar(500) NOT NULL,
`Photo` longblob,
`Photo2` longblob,
`Address` varchar(600) NOT NULL,
`ShowAddress` tinyint(4) NOT NULL,
`ShowExact` tinyint(4) NOT NULL,
`JobStatus` tinyint(4) NOT NULL,
`Longitude` double NOT NULL,
`Latitude` double NOT NULL,
`ClientID` int(11) NOT NULL,
`TradesmanID` int(11) DEFAULT NULL,
PRIMARY KEY (`JobID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=171 ;
and query to create clients table is
CREATE TABLE IF NOT EXISTS `clients` (
`ClientID` int(11) NOT NULL AUTO_INCREMENT,
`FName` varchar(32) NOT NULL,
`SName` varchar(32) NOT NULL,
`Email` varchar(32) NOT NULL,
`HomePhone` int(11) NOT NULL,
`Mobile` varchar(30) NOT NULL,
`Address` varchar(100) NOT NULL,
`County` varchar(32) NOT NULL,
`PostCode` varchar(32) NOT NULL,
`UserName` varchar(32) NOT NULL,
`Password` varchar(32) NOT NULL,
`NotificationID` varchar(255) NOT NULL,
PRIMARY KEY (`ClientID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=96 ;

SELECT
Clients.ClientID
,Clients.NotificationID
,Clients.Email
,Clients.Mobile
,Clients.HomePhone
,Jobs.JobID
,Jobs.Title
,Jobs.Trade
,Jobs.Address AS JobAddress
,Jobs.Urgency
,Jobs.DatePosted
,Jobs.Description
,Jobs.Photo
,Jobs.Photo2
,Jobs.ShowAddress
,Jobs.ShowExact
,Jobs.JobStatus
,Jobs.TradesmanID
,Jobs.Longitude
,Jobs.Latitude
FROM
Clients
INNER JOIN
Jobs
on Clients.ClientId = Jobs.ClientId

How about this? There is a bit of duplication i.e. the select clause in the second union replications the first statement but it will work.
SELECT COL1,COL2, ClientID
FROM Jobs
Union
SELECT ClientID,COL2,COL3
FROM Clients WHERE (Select ClientID FROM Jobs)

try this:
SELECT c.ClientID, c.COL2, c.COL3, x.col1
FROM Clients c inner join
(select clientId,
min(col1) as col1
from jobs
group by clientId) x
on c.clientId = x.clientId

Related

how to match multiple colums of same table and select maximum match colums

SELECT * FROM `student` as s1
LEFT JOIN `student` as s2 ON s1.mainsubject_id=s2.mainsubject_id WHERE s1.mainsubject_id=s2.mainsubject_id AND s1.mainsubject_id > 0 AND s2.mainsubject_id > 0 GROUP BY s1.id`
i have below student table
CREATE TABLE `student` (
`id` int(10) NOT NULL,
`fname` varchar(100) NOT NULL,
`lname` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
`mobile` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
`mainsubject_id` varchar(50) NOT NULL,
`subsubject_id` varchar(50) NOT NULL,
`level_id` varchar(50) NOT NULL,
`date` varchar(100) NOT NULL,
`postcode` varchar(50) NOT NULL,
`gender` int(1) NOT NULL,
`deleted` int(10) NOT NULL,
`temppassword` varchar(100) NOT NULL,
`stripe_id` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
i want to get single record of equal or maximum match from 5 colums (mainsubject_id, subsubject_id, level_id, postcode, gender) in same student table.
what is the best way to get record form same table with maximum match form 5 colums

Subquery as a column : Nested Subquery

I have two tables 'Student' and 'Team'. i want to display count of student according to Team Name, Team_id from team is primary key.
i tried like this
(SELECT(SELECT team,count(*) as team
FROM student GROUP BY team) AS total,
(SELECT team_name
from team WHERE team_id IN (SELECT team
FROM student GROUP BY team)) as team)
i want output as
Team Name Total Student
-----------------------------
Team 1 25
Team 2 10
Table ' Student'
CREATE TABLE IF NOT EXISTS `student` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`rid` varchar(200) NOT NULL,
`sid` varchar(200) NOT NULL,
`name` varchar(600) NOT NULL,
`age` varchar(200) NOT NULL,
`dob` varchar(200) NOT NULL,
`sex` varchar(200) NOT NULL,
`weight` varchar(200) NOT NULL,
`height` varchar(200) NOT NULL,
`team` varchar(600) NOT NULL,
`age_group` varchar(200) NOT NULL,
`weight_group` varchar(500) NOT NULL,
`belt` varchar(200) NOT NULL,
`black_belt` varchar(200) NOT NULL,
`ikata` varchar(20) NOT NULL,
`ikumite` varchar(20) NOT NULL,
`team_kata` varchar(20) NOT NULL,
`special_entry` varchar(20) NOT NULL,
`challange` varchar(50) NOT NULL,
`state` varchar(200) NOT NULL,
`weapon` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
Table 'Team'
CREATE TABLE IF NOT EXISTS `team` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`team_id` varchar(500) NOT NULL,
`team_name` varchar(500) NOT NULL,
`address` varchar(500) NOT NULL,
`coach_name` varchar(500) NOT NULL,
`coach_number` varchar(500) NOT NULL,
`coach_email` varchar(500) NOT NULL,
`gold` varchar(200) NOT NULL,
`state` varchar(500) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
Seems you are looking for this
select a.team_name, count(*)
from team as a
inner join student as b on a.id = b.team
group by a.team_name
(use your proper column for join)

Need suggestion on optimization of MYSQL query

SELECT `tb1`.`id`
FROM `table1` as tb1
INNER JOIN `table2` as tb2 ON tb1.id = tb2.id
INNER JOIN `table3` as tb3 ON tb1.id = tb3.id
WHERE (tb1.item_id = '1')
AND (tb1.user_id = '20')
AND (tb1.type IN ('UPDATE1','UPDATE2','UPDATE3'))
AND (tb1.status = 'DELIVERED')
ORDER BY tb1.date DESC
LIMIT 100
CREATE TABLE `table1` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` INT(11) UNSIGNED NOT NULL,
`item_id` INT(11) UNSIGNED NULL DEFAULT NULL,
`source` ENUM('CPAS','UNIQUE_KEY','BILLING_PARTNER','GAME','MERCURY') NOT NULL,
`date` DATETIME NOT NULL,
`status` ENUM('PENDING','DELIVERED','FAILED','REFUNDED') NOT NULL,
`source_transaction_id` VARCHAR(127) NULL DEFAULT NULL,
`type` ENUM('UPDATE1','UPDATE2','UPDATE3','UPDATE4') NULL DEFAULT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `table2` (
`id_p` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`id` INT(11) UNSIGNED NOT NULL,
`amount` DECIMAL(18,2) NOT NULL,
`old_balance` DECIMAL(18,2) NULL DEFAULT NULL,
`description` VARCHAR(255) NULL DEFAULT NULL,
`date` DATETIME NULL DEFAULT NULL,
`wallet_currency_id` INT(11) NULL DEFAULT NULL,
`wallet_currency_code` VARCHAR(50) NULL DEFAULT NULL,
`wallet_currency_name` VARCHAR(100) NULL DEFAULT NULL,
`type` ENUM('GAIN','SPENT') NULL DEFAULT NULL,
PRIMARY KEY (`id_p`),
INDEX `id` (`id`)
)
CREATE TABLE `table3` (
`id_p` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`id` INT(11) UNSIGNED NOT NULL,
`amount` DECIMAL(18,2) NOT NULL,
`old_balance` DECIMAL(18,2) NULL DEFAULT NULL,
`description` VARCHAR(255) NULL DEFAULT NULL,
`date` DATETIME NULL DEFAULT NULL,
`wallet_currency_id` INT(11) NULL DEFAULT NULL,
`wallet_currency_code` VARCHAR(50) NULL DEFAULT NULL,
`wallet_currency_name` VARCHAR(100) NULL DEFAULT NULL,
`type` ENUM('GAIN','SPENT') NULL DEFAULT NULL,
PRIMARY KEY (`id_p`),
INDEX `id` (`id`)
)
What optimization possible on the above query.
table1 contains more than 500000 rows, table2 and table3 can also have more than 100000 rows.
As per query for particular player and game table1 can have more than 100000 rows.
Is the above query is ok for large large tables or should I split the query in multiple queries.
NDB Engine used.
Please suggest me possible optimization.
Thanks,
Shiv
See comments above, but, at a guess, an index on (item_id,user_id,type,status) might help.

MySQL - Counting a count?

I'm trying to aggregate some data I've pulled from my MySQL db.
Query I'm using is:
SELECT `PUID`,`DROID_V`,`SIG_V`,`SPEED`,
COUNT(distinct IF(sourcelist.hasExtension=1,NAME,NULL)) as Ext,
COUNT(distinct IF(sourcelist.hasExtension=0,NAME,NULL)) as NoExt,
COUNT(distinct NAME) as `All`
FROM sourcelist, main_small
WHERE sourcelist.SourcePUID = 'My_Variable' AND main_small.NAME = sourcelist.SourceFileName
GROUP BY `PUID`,`DROID_V`,`SIG_V`,`SPEED` ORDER BY `DROID_V` ASC, `SIG_V` ASC, `SPEED`
And I wondered if there was a way of counting this result, so I can make a new table that would show me something like:
Every distinct PUID, (count of distinct DROID_V), (count of distinct Sig_V), (SUM of total hits for NAME) WHERE sourcelist.SourcePUID = 'My_Variable' AND main_small.NAME = sourcelist.SourceFileName
As you can see, I'm really not very good at SQL!
Source table:
CREATE TABLE `t1` (
`DROID_V` int(1) DEFAULT NULL,
`Sig_V` varchar(7) DEFAULT NULL,
`SPEED` varchar(4) DEFAULT NULL,
`ID` varchar(7) DEFAULT NULL,
`PARENT_ID` varchar(10) DEFAULT NULL,
`URI` varchar(10) DEFAULT NULL,
`FILE_PATH` varchar(68) DEFAULT NULL,
`NAME` varchar(17) DEFAULT NULL,
`METHOD` varchar(10) DEFAULT NULL,
`STATUS` varchar(14) DEFAULT NULL,
`SIZE` int(10) DEFAULT NULL,
`TYPE` varchar(10) DEFAULT NULL,
`EXT` varchar(4) DEFAULT NULL,
`LAST_MODIFIED` varchar(10) DEFAULT NULL,
`EXTENSION_MISMATCH` varchar(32) DEFAULT NULL,
`MD5_HASH` varchar(10) DEFAULT NULL,
`FORMAT_COUNT` varchar(10) DEFAULT NULL,
`PUID` varchar(15) DEFAULT NULL,
`MIME_TYPE` varchar(24) DEFAULT NULL,
`FORMAT_NAME` varchar(10) DEFAULT NULL,
`FORMAT_VERSION` varchar(10) DEFAULT NULL,
`INDEX` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`INDEX`)
) ENGINE=MyISAM AUTO_INCREMENT=960831 DEFAULT CHARSET=utf8
example records:
5;"v37";"slow";"10266";;"file:";"V1-FL425817.tif";"V1-FL425817.tif";"BINARY_SIG";"MultipleIdenti";"20603284";"FILE";"tif";"2008-11-03";;;;"fmt/7";"image/tiff";"Tagged Ima";"3";"191977"
5;"v37";"slow";"10268";;"file:";"V1-FL425817.tif";"V1-FL425817.tif";"BINARY_SIG";"MultipleIdenti";"20603284";"FILE";"tif";"2008-11-03";;;;"fmt/8";"image/tiff";"Tagged Ima";"4";"191978"
5;"v37";"slow";"10269";;"file:";"V1-FL425817.tif";"V1-FL425817.tif";"BINARY_SIG";"MultipleIdenti";"20603284";"FILE";"tif";"2008-11-03";;;;"fmt/9";"image/tiff";"Tagged Ima";"5";"191979"
5;"v37";"slow";"10270";;"file:";"V1-FL425817.tif";"V1-FL425817.tif";"BINARY_SIG";"MultipleIdenti";"20603284";"FILE";"tif";"2008-11-03";;;;"fmt/10";"image/tiff";"Tagged Ima";"6";"191980"
Sure just do something like
Select Count(*) From (Select * From SomeTable) adummynamesoSqlParserDoesntgetupset
so put your query in parentheses after FROM give it a unique name, and you can treat it like a table or a view.

sql statement mysql notcorrect

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
sql statement mysql
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 ;
Your subquery is missing a From clause:
SELECT SUBSTRING(m.own,3,4) as c
, (select amphur.AMPHUR_NAME
From amphur
Where ??? = SUBSTRING(m.own,3,4) )
, COUNT(* ) AS cnt
FROM MEMBER AS m
However, how does the amphur table relate to the member table?
You cannot use aliases in the same level.
Even if you could, you are filtering on non-correlated columns in your subquery: the subquery would just return the record from amphur if there is one record, or an error if there are more.
Could you please provide some sample data and the desired recordset?
there is no "FROM" clause in your select Amphur.amphur_name