Mysql select * from a where doesnt exist in b - mysql

Im having problems with constructing a query which filters out posts from table a, that already existing in table b.
Table A:
`bok_id` int(6) NOT NULL AUTO_INCREMENT,
`tid` time NOT NULL,
`datum` date NOT NULL,
`datum_end` date NOT NULL,
`framtid` varchar(5) NOT NULL,
`h_adress` varchar(100) NOT NULL,
`l_adress` varchar(100) NOT NULL,
`kund` varchar(100) NOT NULL,
`typ` varchar(5) NOT NULL,
`bil` varchar(99) NOT NULL,
`sign` varchar(2) NOT NULL,
`tilldelad` varchar(12) NOT NULL,
`skapad` datetime NOT NULL,
`endrad` datetime NOT NULL,
`endrad_av` varchar(2) NOT NULL,
`kommentar` text NOT NULL,
`weektype` varchar(3) NOT NULL,
`Monday` tinyint(1) NOT NULL,
`Tuesday` tinyint(1) NOT NULL,
`Wednesday` tinyint(1) NOT NULL,
`Thursday` tinyint(1) NOT NULL,
`Friday` tinyint(1) NOT NULL,
`Saturday` tinyint(1) NOT NULL,
`Sunday` tinyint(1) NOT NULL,
`avbokad` varchar(2) NOT NULL,
`unika_kommentarer` varchar(2) NOT NULL,
UNIQUE KEY `bok_id` (`bok_id`)
Table B:
`id` int(6) NOT NULL AUTO_INCREMENT,
`bok_id` int(6) NOT NULL,
`tid` time NOT NULL,
`datum` date NOT NULL,
`datum_end` date NOT NULL,
`framtid` varchar(5) NOT NULL,
`h_adress` varchar(100) NOT NULL,
`l_adress` varchar(100) NOT NULL,
`kund` varchar(100) NOT NULL,
`typ` varchar(5) NOT NULL,
`bil` varchar(99) NOT NULL,
`sign` varchar(2) NOT NULL,
`tilldelad` varchar(12) NOT NULL,
`skapad` datetime NOT NULL,
`endrad` datetime NOT NULL,
`endrad_av` varchar(2) NOT NULL,
`kommentar` text NOT NULL,
`weektype` varchar(3) NOT NULL,
`Monday` tinyint(1) NOT NULL,
`Tuesday` tinyint(1) NOT NULL,
`Wednesday` tinyint(1) NOT NULL,
`Thursday` tinyint(1) NOT NULL,
`Friday` tinyint(1) NOT NULL,
`Saturday` tinyint(1) NOT NULL,
`Sunday` tinyint(1) NOT NULL,
`avbokad` varchar(2) NOT NULL,
`unika_kommentarer` varchar(2) NOT NULL,
UNIQUE KEY `id` (`id`)
What i want is a query which hides all rows in Table A which exists in table B IF Table B.tilldelad=Requested Date. (f.e 2013-09-30)
Im not sure this makes any sense?
What i want is to filter out period bookings as they have been executed and therefore exists in table b which indicates it has been.
Since its recurring events the same bok_id can exists SEVERAL times in table B, but only ONCE in table A...
SELECT * FROM bokningar
WHERE bokningar.datum <= '2013-09-30'
AND bokningar.datum_end >= '2013-09-30'
AND bokningar.typ >= '2'
AND bokningar.weektype = '1'
AND bokningar.Monday = '1' ## Monday this is dynamically changed to current date
AND bokningar.avbokad < '1'
AND NOT EXISTS ( SELECT 1 FROM tilldelade WHERE tilldelade.tilldelad = '2013-09-30' )
The above code does the trick regarding filtering out rows not in table B, however, if one row in table B has the current date, all results are filtered out.
Only rows with corresponding bok_id's is supposted to get filtered out.
Any thoughts on how to do that? Perhaps a Distinct select?

Typically, NOT IN subselects are quite expensive in querying time. This can normally be handled by doing a LEFT-JOIN and Keeping only those where the other table IS NULL like...
SELECT *
FROM
bokningar
LEFT JOIN tilldelade
on bokningar.bok_id = tilldelade.bok_id
AND tilldelade.tilldelad = '2013-09-30'
WHERE
bokningar.datum <= '2013-09-30'
AND bokningar.datum_end >= '2013-09-30'
AND bokningar.typ >= '2'
AND bokningar.weektype = '1'
AND bokningar.Monday = '1' ## Monday this is dynamically changed to current date
AND bokningar.avbokad < '1'
AND tilldelade.bok_id IS NULL

try something like
SELECT ...
FROM TABLEA
WHERE TABLEA.tilldelad=Requested Date
AND NOT EXISTS (SELECT 1 FROM TABLEB
WHERE TABLEB.tilldelad=Requested Date
AND TABLEA.bok_id = TABLEB.bok_id
AND...)

You need to do set operations.
A simple minus Query would solve your problem here.
Select * from tableA
minus
Select * from tableB;
This would display all data in tableA which is not present in tableB.
You can read more about it over here. http://en.wikipedia.org/wiki/Set_operations_%28SQL%29
For your specific case, try this :
Select * from tableA A
minus
Select * from tableB B where B.tilldelad='3013-09-30';

Related

Creating a summary from two tables?

I have two tables and I'm trying to create a summary with the sum of amount due per person but don't have the creative ID involved.
Table 1:
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`Name` varchar(255) NOT NULL,
`Lname` varchar(255) NOT NULL,
`phone` varchar(15) NOT NULL,
`address` varchar(255) DEFAULT NULL,
`city` varchar(255) NOT NULL,
`state` char(2) NOT NULL,
`zip` varchar(50) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`business_name varchar(255) DEFAULT NULL,
Second table:
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`C_ID` INT(10) UNSIGNED NOT NULL,
`Amount_Due` DECIMAL(7 , 2 ) not null DEFAULT 0,
`created_date` DATETIME NOT NULL,
`closed_date` DATETIME default NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=LATIN1;
Here is what I'm trying to do:
I'm trying to make a summary with dates within 5/1/18 and 6/15/18.
Have the sum of due amount for each person
Have these aliases : Business name, Phone Number ,Invoiced Amounts
I'm trying to test my code but i'm getting errors:
SELECT Name,phone,SUM(Amount_Due) FROM test_customer,test_invoices
WHERE created_date BETWEEN '2018-05-01' AND "2018-06-15'
If I understand correctly, you need to use JOIN instead of ,(CROSS JOIN) and GROUP BY in non-aggregate function columns.
SELECT Name 'Customer Name',
phone 'Phone number',
SUM(i.Amount_Due) 'Amount due'
FROM test_customer c
INNER JOIN test_invoices i ON C.id = i.C_ID
GROUP BY Name,phone
sqlfiddle

MySQL count giving different result everytime

I am using a MySQL query to calculate the number of records based an certain where conditions. But each time the website/database is used by multiple users from multiple systems. The count result varies dramatically.
I have tried to find a lot but could not figure the issue. I am attaching the screen-shots. Please help!
Here's the screenshot: https://drive.google.com/file/d/0B4kI3aImsSLmaGN3cXo1Rk1saDg/view?usp=sharing
Structure of table:
CREATE TABLE IF NOT EXISTS `rap_partmpuser` (
`par_tmp_track` int(11) NOT NULL,
`par_tmp_exam` int(11) NOT NULL,
`par_tmp_user` int(11) NOT NULL,
`par_tmp_status` varchar(100) NOT NULL,
`par_tmp_sdate` date NOT NULL,
`par_tmp_edate` date NOT NULL,
`par_tmp_adate` date NOT NULL,
`par_tmp_tdate` date NOT NULL,
`par_terdate` date NOT NULL,
`par_tmp_score` int(11) NOT NULL,
`par_tmp_tques` int(11) NOT NULL,
`par_tmp_uopt1` varchar(250) NOT NULL,
`par_tmp_uopt2` varchar(250) NOT NULL,
`par_tmp_uopt3` varchar(250) NOT NULL,
`par_tmp_uopt4` varchar(250) NOT NULL,
`par_tmp_uopt5` varchar(250) NOT NULL,
`par_tmp_uopt6` varchar(250) NOT NULL,
`par_tmp_uopt7` varchar(250) NOT NULL,
`par_tmp_uopt8` varchar(250) NOT NULL,
`par_tmp_uopt9` varchar(250) NOT NULL,
`par_tmp_uopt10` varchar(250) NOT NULL,
`par_tmp_uopt11` varchar(250) NOT NULL,
`par_tmp_uopt12` varchar(250) NOT NULL,
`par_tmp_uopt13` varchar(250) NOT NULL,
`par_tmp_uopt14` varchar(250) NOT NULL,
`par_tmp_uopt15` varchar(250) NOT NULL,
`par_tmp_filter` int(11) NOT NULL,
`par_tmp_sfilter` int(11) NOT NULL,
`par_tmp_cpyid` int(11) NOT NULL,
`par_tmp_usrid` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
And my query is
SELECT count(*)
FROM `rap_partmpuser`
where par_tmp_cpyid='10106' and par_tmp_track='458'
and par_tmp_usrid='771' and par_tmp_status='Active'
Sometimes it gives me the result 46, sometimes 26, sometimes 70. The correct result is 70. Why is it different sometimes?
I think the answer is simple. I'm not sure that this is the only mistake.
SELECT count(*) FROM rap_partmpuser where par_tmp_cpyid=10106 and
par_tmp_track=458 and par_tmp_usrid=771 and par_tmp_status='Active';
Don't use 'single quotes' to check integer values like par_tmp_cpyid='10106'.Try above query if you get correct answer comment below.
$reponse = $bdd->prepare('
SELECT count(*)
FROM rap_partmpuser
where par_tmp_cpyid=10106 and par_tmp_track=458
and par_tmp_usrid=771 and par_tmp_status=\'Active\'');
$reponse->execute();
$donnees = $reponse->fetch();
echo $donnees[0] ;

query taking too much time with irritated performance [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
My query is taking very long time around 1hr
it was impacting on DB performance.
even for Explain plan also it was taking very long time.
please rewrite the query for optimal performance.
Query:
SELECT Count(*)
FROM (SELECT paid.keyword_id,
paid.keyword_name,
stat.orgentrances,
keyword.rank1,
keyword.rank_check,
Sum(paid.clicks) AS sumclick,
Sum(paid.clicks * paid.avg_position) AS
sumclickavgpos,
Sum(paid.itemrevenue) AS sumitem,
Sum(paid.cost) AS sumcost,
Sum(paid.transactions) AS sumtrans,
Sum(paid.impressions) AS
sumimpress,
IF(Sum(paid.impressions) = 0, 0, Sum(
paid.impressions * paid.avg_position) / Sum
(paid.impressions)) AS
sumimpressavgrank,
con.item_revenue,
con.transactions,
keyword.monthly_search_volume
FROM `t_keyword_paid_analytics_google_ib` paid
LEFT JOIN (SELECT outer_t.keyword_id,
Sum(outer_t.item_revenue) AS item_revenue,
Sum(outer_t.transactions) AS transactions
FROM t_keyword_conversion_ga_ib outer_t
WHERE outer_t.own_domain_id = 720
AND outer_t.traffic_date >= '2012-12-01'
AND outer_t.traffic_date <= '2012-12-31'
GROUP BY outer_t.keyword_id) con
ON paid.keyword_id = con.keyword_id
LEFT JOIN (SELECT outer_t.keyword_id,
Sum(outer_t.entrances) AS orgEntrances
FROM t_keyword_stat_ga_ib outer_t
WHERE outer_t.own_domain_id = 720
AND outer_t.traffic_date >= '2012-12-01'
AND outer_t.traffic_date <= '2012-12-31'
AND ( outer_t.medium = 'organic'
OR outer_t.medium IS NULL )
GROUP BY outer_t.keyword_id) stat
ON paid.keyword_id = stat.keyword_id
LEFT JOIN `t_managed_keyword_ib` keyword
ON keyword.id = paid.keyword_id
WHERE paid.own_domain_id = 720
AND paid.traffic_date >= '2012-12-01'
AND paid.traffic_date <= '2012-12-31'
AND ( paid.channel IS NULL
OR paid.channel = 'Google' )
GROUP BY paid.keyword_id
HAVING paid.keyword_id IS NOT NULL) tempt;
(outer_t.medium = 'organic' or outer_t.medium is null ) after outer_t.own_domain_id = 720 and (paid.channel is null or paid.channel = 'Google')
after paid.own_domain_id = 720
Tables Structure:
mysql> show create table t_keyword_paid_analytics_google_ib\G
*************************** 1. row ***************************
Table: t_keyword_paid_analytics_google_ib
Create Table: CREATE TABLE `t_keyword_paid_analytics_google_ib` (
`keyword_name` varchar(255) DEFAULT NULL,
`id` int(11) NOT NULL,
`keyword_id` int(11) NOT NULL,
`target_url_id` int(11) DEFAULT NULL,
`own_domain_id` int(11) NOT NULL,
`log_date` date NOT NULL,
`traffic_date` date NOT NULL,
`impressions` int(11) DEFAULT NULL,
`clicks` int(11) DEFAULT NULL,
`entrances` int(11) DEFAULT NULL,
`match_type` int(11) DEFAULT NULL COMMENT '1: Phrase, 2: Exact, 3:Broad 4: etc',
`ad_group_name` varchar(200) DEFAULT NULL,
`ad_distribution_network` varchar(500) CHARACTER SET latin1 DEFAULT NULL,
`match_query` varchar(500) CHARACTER SET latin1 DEFAULT NULL,
`cost` decimal(10,2) DEFAULT NULL,
`cpm` decimal(10,2) DEFAULT NULL,
`ctr` decimal(10,2) DEFAULT NULL COMMENT 'percent',
`cpc` decimal(10,2) DEFAULT NULL,
`campaign` varchar(200) CHARACTER SET latin1 DEFAULT NULL,
`keyword_status` tinyint(4) DEFAULT NULL COMMENT '1: Active,2: Approved, 3: Disapproved, 4: Paused , 5:Pending, 6: Failed, 7:etc',
`ad_group_status` tinyint(4) DEFAULT NULL COMMENT '1:ELIGIBLE; 2=PAUSED;3=LOW_SEARCH_VOLUME;4 =LOW_QUALITY_SCORE; 5=DISAPPROVED; 6=AD_GROUP_PAUSED; 7=etc',
`max_cpc` decimal(10,2) DEFAULT NULL,
`quality_score` tinyint(4) DEFAULT NULL,
`channel` varchar(100) DEFAULT NULL,
`first_page_cpc` decimal(10,2) DEFAULT NULL,
`avg_position` decimal(10,2) DEFAULT NULL,
`itemRevenue` decimal(10,2) DEFAULT NULL,
`goal1value` decimal(10,2) DEFAULT NULL,
`goal2value` decimal(10,2) DEFAULT NULL,
`goal3value` decimal(10,2) DEFAULT NULL,
`goal4value` decimal(10,2) DEFAULT NULL,
`transactions` int(10) DEFAULT NULL,
`goal1completions` int(10) DEFAULT NULL,
`goal2completions` int(10) DEFAULT NULL,
`goal3completions` int(10) DEFAULT NULL,
`goal4completions` int(10) DEFAULT NULL
) ENGINE=BRIGHTHOUSE DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql> show create table t_keyword_conversion_ga_ib\G
*************************** 1. row ***************************
Table: t_keyword_conversion_ga_ib
Create Table: CREATE TABLE `t_keyword_conversion_ga_ib` (
`keyword_name` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`id` int(11) NOT NULL,
`own_domain_id` int(11) DEFAULT NULL,
`keyword_id` int(11) DEFAULT NULL,
`traffic_date` date DEFAULT NULL,
`targeturl_id` int(11) DEFAULT NULL,
`entrance` int(11) DEFAULT NULL,
`transactions` int(11) DEFAULT NULL,
`item_revenue` decimal(9,2) DEFAULT NULL,
`goal1completions` int(11) DEFAULT NULL,
`goal2completions` int(11) DEFAULT NULL,
`goal3completions` int(11) DEFAULT NULL,
`goal4completions` int(11) DEFAULT NULL,
`goal5completions` int(11) DEFAULT NULL,
`goal6completions` int(11) DEFAULT NULL,
`goal7completions` int(11) DEFAULT NULL,
`goal8completions` int(11) DEFAULT NULL,
`goal9completions` int(11) DEFAULT NULL,
`goal10completions` int(11) DEFAULT NULL,
`goal1Value` decimal(9,2) DEFAULT NULL,
`goal2Value` decimal(9,2) DEFAULT NULL,
`goal3Value` decimal(9,2) DEFAULT NULL,
`goal4Value` decimal(9,2) DEFAULT NULL,
`goal5Value` decimal(9,2) DEFAULT NULL,
`goal6Value` decimal(9,2) DEFAULT NULL,
`goal7Value` decimal(9,2) DEFAULT NULL,
`goal8Value` decimal(9,2) DEFAULT NULL,
`goal9Value` decimal(9,2) DEFAULT NULL,
`goal10Value` decimal(9,2) DEFAULT NULL,
`medium` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`source` varchar(255) COLLATE latin1_bin DEFAULT NULL
) ENGINE=BRIGHTHOUSE DEFAULT CHARSET=latin1 COLLATE=latin1_bin
1 row in set (0.00 sec)
please help me here.
You don't indicate any indexes. You will need them to improve performance.
OK, I don't understand the part after the semi-colon, so I'll ignore that.
Your whole query does a count(*) of a sub select, but you are doing a bunch of aggregation in the sub-query (calling SUM) which is work the database doesn't have to do, so eliminate that.
You do aggregation in further sub-queries, so cut that out.
In your main sub-select, where you are just doing a count(*), you select fields that are not in your group by, which is work on the database that is not affecting the count, so get rid of that.
That gives us something like...
select count(*)
from (select paid.keyword_id
from `t_keyword_paid_analytics_google_ib` paid
left join
(select outer_t.keyword_id
from t_keyword_conversion_ga_ib outer_t
where ...
group by outer_t.keyword_id ) con
on paid.keyword_id = con.keyword_id
left join
(select outer_t.keyword_id
from t_keyword_stat_ga_ib outer_t
where ...
group by outer_t.keyword_id) stat on paid.keyword_id = stat.keyword_id
left join `t_managed_keyword_ib` keyword on keyword.id = paid.keyword_id
where ...
group by paid.keyword_id
having paid.keyword_id is not null
) tempt;
Also, counts may be faster than selects and you seem to be looking for the keyword_id and counting them in the end. If you can count as you go that would be better than selecting. I'm not sure if you can apply that to this query, but keep it in mind.
You have to add some index to your tables,
ALTER TABLE table_name ADD UNIQUE INDEX cate_id(field_name);
here is the documentation
In your case I would add index to:
paid.keyword_id
con.keyword_id
stat.keyword_id
keyword.id
paid.keyword_id
because you are making join on those fields

Mysql Join / Union issue

I am trying to create a report that pulls data from 2 tables: a & b. The report is grouped by a.clock. Most of the data for the report comes from a - that part is working fine. a.clock links with b.userID.
The part i am struggling with is for one of the columns the data comes from b. I need to total up the following for each a.clock grouping in the main report (this query works standalone)
SELECT (
SUM(
TIME_TO_SEC(
TIMEDIFF(
CONCAT(b.endDate, ' ', b.outTime),
CONCAT(b.startDate, ' ', b.inTime)
)
) / 3600
)
) AS 'Misc Hours' FROM b
In other words, i need to total the Misc Hours (in b) for each a.clock. I thought maybe joining the b table was necessary but that didn't seem to work. Any suggestions?
Here are the table definitions (sorry, verbose)
CREATE TABLE `a` (
`laborID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`type` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '0=sched; 1=accepted; 2=complete; 3=authorize',
`laborType` varchar(15) NOT NULL DEFAULT '0' COMMENT 'Lookup',
`hours` varchar(15) NOT NULL DEFAULT '',
`wage` varchar(15) NOT NULL DEFAULT '',
`id` int(10) unsigned NOT NULL DEFAULT '0',
`laborDate` date NOT NULL,
`ot` varchar(15) NOT NULL,
`clock` varchar(15) NOT NULL,
`setup` varchar(15) NOT NULL DEFAULT '',
`ot2` varchar(15) NOT NULL,
`wageOT1` varchar(15) NOT NULL,
`wageOT2` varchar(15) NOT NULL,
`inTime` varchar(15) NOT NULL,
`outTime` varchar(15) NOT NULL,
`startDateString` varchar(125) NOT NULL,
`endDateString` varchar(125) NOT NULL,
`authRequestDate` datetime NOT NULL,
`authRequestUser` int(10) unsigned NOT NULL,
`authRequestReason` text NOT NULL,
`authDate` datetime NOT NULL,
`authUser` int(10) unsigned NOT NULL,
`authRequired` varchar(45) NOT NULL,
`km` varchar(45) NOT NULL,
`travelTime` varchar(45) NOT NULL,
`actualInTime` varchar(45) NOT NULL,
`actualOutTime` varchar(45) NOT NULL,
`actualKm` varchar(45) NOT NULL,
`actualTravelTime` varchar(45) NOT NULL,
`intNotes` text,
`extNotes` text,
`billableReason` text,
`billableHours` varchar(45) DEFAULT NULL,
`actualHours` varchar(45) DEFAULT NULL,
`overtime` varchar(45) DEFAULT NULL,
`followUpReason` text NOT NULL,
`responseType` varchar(45) DEFAULT NULL,
`followUpType` int(10) unsigned DEFAULT NULL,
`billableDrop` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`laborID`),
KEY `id` (`id`),
KEY `type` (`type`),
KEY `laborDate` (`laborDate`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC;
CREATE TABLE `b` (
`timecodeID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`userID` int(10) unsigned NOT NULL,
`inTime` varchar(45) NOT NULL,
`outTime` varchar(45) NOT NULL,
`startDateString` varchar(145) NOT NULL,
`endDateString` varchar(145) NOT NULL,
`startDate` varchar(45) NOT NULL,
`endDate` varchar(45) NOT NULL,
`type` varchar(45) NOT NULL,
`comments` text NOT NULL,
`multiDay` tinyint(3) unsigned NOT NULL,
PRIMARY KEY (`timecodeID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Well if you have multiple rows in each of the table then you will get larger sum as each row in labor will join with each row in timecodes. So the idea is to have nested query group by clock and userid to get one row each as per the groupping
SELECT A.Clock, A.hours, B.'Misc Hours'
FROM
(
SELECT labor.clock,
SUM(hours) Hours
FROM labor
GROUP BY labor.clock
) A
INNER JOIN
(
SELECT timecodes.userID,
(SUM(
TIME_TO_SEC(
TIMEDIFF(
CONCAT(timecodes.endDate,' ',timecodes.outTime),
CONCAT(timecodes.startDate,' ',timecodes.inTime)
)
)/3600
)
) AS 'Misc Hours'
FROM timecodes
GROUP BY timecodes.userID
) AS B ON A.Clock = B.UserId
SELECT timecodes.userID,
(SUM(
TIME_TO_SEC(
TIMEDIFF(
CONCAT(timecodes.endDate,' ',timecodes.outTime),
CONCAT(timecodes.startDate,' ',timecodes.inTime)
)
)/3600
)
)
AS 'Misc Hours'
FROM timecodes WHERE timecodes.userID=labor.clock GROUP BY labor.clock

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.