Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    MYSQL




Compare Count Records


I need some help with some SQL I need to write.
Here is what I'm trying to do:
I have 2 tables in the same format. Table B is a subset of Table A.
For each key value I need to count how many records I have in table A and in table B and output the entries that are missing from table B.

E.g. for key 100 table A has 5 records and table B has only 2.
I need to output the missing 3 records.




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Compare Text Fields Of Two Records?
If SELECT DISTINCT FieldName will cull the identical records of a certain field and show a list of the unique values, is there a way to go a step further and indicate what the differences are in that field between any two specified unique records? I am looking for a way to look at blocks of text (articles undergoing editing) up to 2000 words to see what has been added or removed from one record to the next.

Is this possible? If not in MySQL, I can work with VBScript or VB6 if you know of a method for those.

Count Related Records, But Show Records With NO Related Records Also
cl_items
========
it_id (pk)
it_ownerid
it_name

cl_offers
========
off_id (pk)
off_itid (fk) -> to it_id
off_whoid

My query needs to output ALL of the records in cl_items AND still show how many offers are on each item (from cl_offers)

I can't get what I want through the GROUP BY because I want to show the records in cl_items which DO NOT have any offers on them yet also.

Is there any way to do this with mysql only?

Count Records Per Day And Show Max
I've been using basic sql for a while and always used PHP to get round
complicated queries but now I need to see if I can create a query that
will show me the busiest day in our helpdesk and return the date and
the number of calls.

We have a database with a table called tracking which has date fields.
I want to count all teh records for each day, and return date with the
highest count and the count itself...

I'm sure its possible but I'm stuck as to what to google for...

Count Records Within Query
I have the following information in table 'Test1':

field1  field 2
000001  000000
000002  000001
000003  000000
000004  000000

I want to write a query that will only display the unique field 2 records and as well provide a count of how many records have that id.

output should look like

field2  recno
000000    3
000001    1

can some one help with the trick to make this work?

Count Records In 2nd Table
Create a result set with all records from table A and counted related records in table ....

Count Records From Two Tables
I have two tables and I want to count the number of records where field1 = 1.

tbl1    field1
           1
           2
           1
           0

tbl2    field1
           0
           0
           1

Result should be 3

Can someone help with the right coding?

How Do I Do A Count Of How Many Records Are Tagged Under One Tag?
How do i do a count of how many records are tagged under one tag?

SELECT DISTINCT ut.uxtagid, ut.uuserid, ut.uprivate, tt.tid, tt.ttag, tt.trated FROM usertags  ut, thetags tt WHERE ut.uuserid=1 AND ut.uxtagid=tt.tid ORDER by tt.ttag

"thetags" contains the actual tag name like this:

tid, ttag

"usertags" when a user tags a record its stored here, and the records here are linked to the tag name

uuserid = the user who tagged it
uxtagid = is joining the "thetag" table to identify the name of the tag
uxforumpostid = the record that is being tagged

How Count Records From Two Tables?
I’m having two tables: ap_author and ap_books. I wont count how many records have everyone authors in table ap_books.

ap_author ------------------------------
| author_id | first_name | last_name |
| 1 | first1 | last1 |
| 3 | first2 | last2 |
| 4 | first3 | last3 |
--------------------
ap_books-------------------------------------
| book_id | title | author_id | total_pages |
| 1 | title1 | 1 | 2 |
| 2 | title2 | 1 | 5 |
| 3 | title3 | 4 | 1 |
--------------------

I use this query: SELECT ap_author.author_id, ap_author.first_name, ap_author.last_name, COUNT(*) AS books FROM ap_author, ap_books WHERE ap_author.author_id=ap_books.author_id GROUP BY ap_author.author_id

This query returns only these authors, which have one and more records in table ap_book. How I’m can get all author?

Count And Arranging Records
I need counting the number of records each user has and arranging their outputs.

This is my sample table:
uname | uid
-------------
"bob""1"
"bob""1"
"bob""1"
"james""7"
"james""7"
"lars""11"

I'm trying to select the uname and uid from this table based on which users have the most records in there.

So in this case the output would be

"bob""1" "3"
"james""7" "2"
"lars""11" "1"

Bob will be top since he has 3, james will be next and so on. The 3 2 1 you see is the number of records each has.

Count Deleted Records
Is there a way I can count/display the number of records affected by a delete statement on my webpage.
I currently have a delete statement:
DELETE FROM applications WHERE Date < whenever

can I combine this with a COUNT ??

Get List (count) Of Records
I want to get a list of records with duplicate (or triplicate, etc.) fields.
I tried:

SELECT *, count(*) as q FROM test WHERE q > 1 GROUP BY field1;

This didn't work because "q" is regarded as a missing column. Is there a
way to do this in a single SQL statement?

Get Count Of Matching Records
I have two tables, that I am trying to match up rows and count the number of times first_name AND last_name in table A match first_name AND last_name in table B. What is the best way to do this without creating a mess of queries and code?

Getting A Count Of Records Based On Inner Relationships
I am making a PHP-MySQL forum. I have a table "forum" that holds all the posts.

Fields in forum: ID, user, title, body, threadID, datePosted.

All top level posts (ie, original posts for a thread) have a threadID=0. All other posts are comments, hence their threadID is set equal to the 'ID' of the original thread post.

I can get a list of the last 10 top level thread posts:
select * from forum where threadID=0 order by ID desc limit 0,10;

Also, I can get the # of comments for an individual thread (for instance, thread # 37):
select count(*) as Expr1 from forum where threadID=37;

How in the heck do I form those two statements into one statement? In my main forum view where I list the last 10 threads, I want returned in each result set the ID, title, user, and the # of comments posted in that thread.

Sql Query For Count Records Of Current Week
I have a registration form that saves the registration date on a datetime column. I would like make a query to know how many records are in the table that where for the current week. For example, I'm on week 25 and I want to know how many records I have for this week without the need of specifying the from and to dates. Is there a way to do this?

Select / Count Not Showing Records ... Permissions?
I imported 600,000 records from SQL Server 2005 to mySQL.

I know they are in there -- I just can't see them!

When I do a SELECT, it processes for a while, and then returns nothing.

Same with a count, it sits there processing, and then returns a '0'

However when I do the same on a table I know has no rows, it definitely
comes back right away.

I'm logged in as root. Do I need to assign myself some permissions to
see these records? And the count numbers?

Join Tables To Count Number Of Records?
I've two tables:

responses
==========
response_id
schema_id
timestamp

answers
==========
answer_id
response_id
answer

(that's a cut down version, but will do for this question)

I need to get all the responses where there is at least one answer in the answers table. But I do not want the answer data. It's literally a quick check for an export to say "get me all the responses where there's at least one question answered". I have this: Code:

Wot Is The Function To Count The Records In Selelct Statmtn In MySQL
wot is the function to count the records in Selelct statement in MySQL?

Just like
PHP

mysql_num_rows($result);

in php.

Slow Select Using Count(distinct) In A Table Bigger Than 100000 Records
Recently I started using MYSQL in my enterprise. I made a table which has around 100000 records. The problems is that it is really slow.. Im trying to do a query in which I get the number of distinct users per day.

This is my query:

select date(startedDate) as mydate, count(distinct(Users)) as users from Mytable
group by mydate

It is really simple and it does it correctly but it takes one minute.. One minute is not too much time but i need to insert around 10 000 000 records and thats what worries me.....

#1136 - Column Count Doesn't Match Value Count At Row 1
I'm getting the above error with the following SQL Statement. I cannot seem to find the error in the code. The select statement does pull multiple rows.

I'm using my SQL version: 4.1.19

Any clue on why this isn't working?

INSERT INTO `Grants` ( `Project_Code` , `Grant_Code` , `Fiscal_Year` , `Capital` )
VALUES (
(
SELECT Project_Code, Grant_Code, FY, SUM( Capital )
FROM Grants_Temp
WHERE Project_Code = 'OSUT'
AND FY = '2006'
AND Claim_Month = '072006'
GROUP BY Grant_Code
ORDER BY Grant_Code
)
)

Compare Row 1 With Row 2
I have a table with data that is supposed to only increase in value, but due to bugs outside my control, I need to check the entries in the database on a periodic basis.

I can of course do this in a scripting language like php but I was wondering if there is a way to do it in MySQL with some kind of SELECT query reducing the server load?

the table looks like:
id auto_increment
timestamp
data (numeric value that will increase with time)

What I'm looking for is something like this
SELECT id FROM table WHERE data is less than previous row's data ORDER BY timestamp

Compare
How can I stop a function in PHP?

When comparing data I want to stop the function on a hit.



Function compare($data) {

For ($n=1; $n<=10000; $n++) {

For ($m=1; $m<=10000; $m++) {

If $data[$n]==data[$m] {

Compare Tables
Does anyone know if there is any tool that can compare two databases (and the tables) and allow you to create the SQL necessary to make them identical? I.e. creating new tables, adding columns,

How To Compare Different Datatypes?
how to create user except root account ?


Compare Dates
Using mysql 4.0, which doesn't support DATEDIFF

I'd like to compare two dates and check if the difference is 6 weeks or greater. How can I do this?

Compare 2 Databases
I have 2 CMS Systems running on MySql
I was upgrading one of them.
During the update(s), i messed some fields-defs at some tables.

Is there any possibilities to compare those two databases to see, which Field-Attributes are different (Just Name, Type) and which Tables are different (has table1 on DB1 other fields than table1 on DB2)

Compare Strings In SQL
I have a string field where are stored dates and i want to select from this table like this:

"SELECT * FROM [table] WHERE field_date>='date1' AND field_date<='date2'".

Apparently doens't work, the query returning all the rows in the table. Is there a posibility for this string comparison or i should probably convert the field that stores these dates into the date one?

Compare Two Tables
I have two tables.

First table is just list of dealers (table name dealers)

Second table is user input of dealers transactions.(Table name is log) The first field of table two is the dealership name (field name is dealership).

Sometime not all dealers in the first table perform transactions.

How can show the dealers that are listed in the first table that are not listed in the second table.

I tried this, but it did not work

select dealers from dealers, log where dealers != dealership

How To Compare 2 Tables ?
I have two tables containing references and I would like to compute the differences but I have some problems. For example

table 1

+--------+
| ref |
+--------+
| A250 |
| B260 |
| G650 |
| D250 |
+--------+

table 2

+--------+
| ref |
+--------+
| A250 |
| B280 |
| Z650 |
| D250 |
| W650 |
+--------+

SELECT table1.`ref` FROM table1, table2 WHERE table1.`ref` NOT LIKE table2.`ref` group by table1.`ref` ;

does not compute corectly the différences.

On the other end :

SELECT table1.`ref` FROM table1, table2 WHERE table1.`ref` LIKE table2.`ref` group by table1.`ref` ;

computes correctly the comon references.

So what is wrong in the first query ? What can I do to see the differences between my 2 tables ?.

How Do I Compare Two Tables?
I have two tables with the same columns.Table A and Table B. I would like to compare a column in bot tables and determine if there is an entry in A but not in B, also I'd like to determin if there is an entry in B but not in A. How would I do this?

Compare Schema
I have two MySQL database. How can I compare the schema. I wan't to see if there are any differences in them. Is there a tool for this?

Date Compare
I need to compare dates without the time. In oracle, I would use a trunc() function to drop the time. How is this done in MySQL?

LIKE Compare Two Columns
I have a issue comparing two columns.

I am using a select query, and here it is:

SELECT project_name AS project, project_manager AS manager, percent_complete AS completed, plcp_phase AS phase, (

SELECT COUNT( * ) FROM meetings WHERE project_name = project) AS meetings,

(SELECT COUNT( * ) FROM filesdocs WHERE product LIKE project) AS files,

(SELECT COUNT( * ) FROM issues WHERE project_name = project) AS issues

FROM projects GROUP BY project");

I have several tables, projects, whehere i obtain the project name, filesdocs, where i am looking for the amount of files for each project, and the same with issues and meetings.

In filesdocs, product name could be "xxx", or "yyy", or any other project name, OR "xxx yyy" (with a space), becuase that file is relavant for TWO projects. Which would mean if i LIKE seach for "%xxx%", then the files with the products named "xxx" AND "xxx yyy" will get returned. Works great if i supply the project name as variable, since i can quote the variable as "%variable%".

However, in the search above where i the seach finds all the different projects in one search, how can i do a LIKE seach with %'s on the column alias 'project'? Although the alias "project" LIKE product works, it does not return the count of files where products is in the form of "xxx yyy", as obviously if the current project in the sql search = "xxx", then "xxx" obviously does NOT equal "xxx yyy" and the count misses a file. But i cannot find a way of using the alias with % % marks encapsulating it. Code:

Compare Data
I want to compare data in a single table, and get the index number as well (if these columns have same data/or under the condition i setup).

example:

table:
index ---- col1
1 ---- 260
2 ---- 240
3 ---- 330

I would like to compare these col1 data and return the number(s) which is the largest (the other case is +/- 30 of the smallest). And return the index of the number(s) too.

Compare Tables
I am looking to figure out a way to compare two tables and show me what is different.
One table is 'usertable'.This table contains 5 columns reference(key),tag,datatype,scantime,description. When I run my application,it takes a snapshoot of 'usertable' and create a temp table 'temptags'.Every couple of minutes I want to compare the two tables and determine what is different. Fist off,I want to compare the reference column of each table and determine if there is a new reference index or if any have been deleted.Then I want to look at all the other column and determin if any have changed.

These two tables are located in different databases 'usertable' is located in 'user' database and 'temptags' is located in 'tags' database.

Compare Two Date
i have a table with txn_id, txn_name,txn__date.

now i want get the all txn_id's whose txn_date is in between 01-01-01 and 02-02-02.

Compare Two Datasets
I have two tables, TableA and TableB.

TableB holds a copy of some records in TableA. TableB is read only.

TableA is live--rows are added, deleted, and updated regularly.

I need to write a script to see if rows from TableA which correspond to rows that exist in TableB (by corresponding ID field) have been modified, and if they have, I need to copy the new version of the row from TableA to TableB.

My first guess was to do some sort of LEFT JOIN WHERE NULLs existed after comparing each column for exact match. Ugly.

My second guess was to do:

INSERT INTO TableB ([cols])
SELECT * FROM TableA WHERE NOT EXISTS (SELECT * FROM TableB WHERE col1=TableA.col1, col2=TableA.col2, etc.).

Compare Two Tables
there is a way to compare fields and rows in two tables and display the values that differ from each other.

Example: I have a table called "products", and a table called "products_bak", with a few fields: id, name, quantity. "Products_bak" contains yesterdays values, and "products" contains todays updated values.

"Products_bak.quantity" contains yesterday's stock, and "products.quantity" contains current stock. Is there a way to compare the two tables and get the differences. I would like to be notified that the stock has increased from 3 to 5 in the first row, and that the name has changed from "TestOld" to "TestNew".

Compare Two Dates
I have a field in my database called rntl_date and this contain the date when the apartment will be available.
I want to compare this with Curdate() and if this is less then Curdate() to show Availabe
For example:

rntl_data curdate()
2004-12-12 2005-10-10 ->Available
2006-12-12 2005-10-10 ->Non available

Compare Date
The way the current script was written was to write the date in a varchar field in the format 2008-02-12 18:22:15

I wish to take the number days before expiry for the expiry column and compare it.
Is that possible in a field that was not formated for date?
If date+expiry = today then run this other script.

Compare Values
What I need is a mysql query where when given a value, a message will be returned if the value in the table is plus or minus 15 from the new value that is being entered. Sort of comparing the 2 but the new value is going into a different table. I do not even know where to begin!
Such as

select weight from 'table' if $weight is + or - 15

Compare Two Fields
I have a simple table that I need to compare the TL fileds and produce a report only if the TL fields matches and the Status feild equals Buy and Sell.

I want to match any inventory that is the same TL and I am buying, selling, and report that from my database. I am copying a simplified version of the table as an example.

I am using PHP and MySQL as tools and cannot figure out how to comapire all the fields, as I am a rookie at best.

IDRefNoTLStatusStatus_Date
5625DryVanSell2006-06-21
5725DryVanBuy2006-06-30

Compare 2 Databases
How can I compare 2 databases to check if one is embedded within the other . That mean given a database A , add some tables and new records to it , modify some tables , that result in table B . How can I check that A is embedded with B ? that all A entries and part of B ? . Any easy tools to do that ?

Compare Datetimes
How do you compare two datetimes to find out which is earlier or later? I've tried the following (log.time_added is a datetime):Code:

...where log.time_added >= FROM_UNIXTIME( 1163656800 )
AND log.time_added <= FROM_UNIXTIME( 1163743200 )

and it returns all results from any date, including dates not in the specified range. I have confirmed that FROM_UNIXTIME is working correctly, it just isn't comparing the dates correctly.

Extract And Compare
I was requested to do a report which consists in:
Report total sales (for example) in month: XX
Now, The user has 2 options:
- Actual month (default)
- A month choosen from (month name? or month number?)
I'd like you to help me in these 2 steps...
1. How to get records which were "saved" in actual month ?
2. How to compare and make queries if user selects month name or month number ?

Compare Two Tables
How can I compare two tables and receive only the entry that are not the same as the first table.

Example:

First Table (fruit); contain apple, orange, lemon and lime. Second table (remove); orange, lime.
I need to have only apple and lemon as the result of my SQL SELECT.

Compare New Data
I have a bunch of feeds and I was wondering how I would compare the old one with the new one and then report the difference? For example, one item I would like to see is if changes were made to a products price like in the example below

Old Data

1 Blah Blah item image.jpg description $24.99
2 New Example pictur.jpg summary $99.99

New Data

1 Blah Blah item image.jpg description $14.99
2 New Example pictur.jpg summary $99.99

Since #1's price was changed, after I upload the information I would like it to say item one's price was change by $10 and is now $14.99. How would I do this or what technique would I use?

Column Count Doesn't Match Value Count At Row 1
I am getting the following error when I run my query.
Column count doesn't match value count at row 1

I have looked up this error and have checked and I appear to have the right number and names in my query. In the DB table I have 34 columns, and that is what I have in the query.

The last 4 in the query are for the names of the images being uploaded, but am not sure how this all works, so I don't know if I need them, but have them there until I know for sure. Comment, Purchase and Remarks are not used in this form, but I have added them so everything is being shown in the query. Code:

Count(*) As Count And TotalCount Of Count
data in myTable1

(n) country_id
(1) 3
(2) 1
(41) 1
(5) 2
(6) 3
(7) 4

data in myTable4

(time) param
(10 : 10) c=4
(10 : 12) c=2
(10 : 30) n=41&k=5
(10 : 35) c=1
(10 : 37) n=5
(10 : 50) c=2
(10 : 54) c=2
(10 : 55) n=1&cate=6
(11 : 12) c=2
(11 : 15) n=7
(11 : 20) c=1
I have data in myTables like the above.

I have the following code.

code

(select left(time,2) as hour, count(*) as count
from myTable4
where

left(param,1)='c'
and
substring(param,3,1)=2

group by left(time,2)
)
UNION all
(select left(time,2) as hour, count(*) as count
from myTable4,myTable1

where
left(param,1)='n'
and
substring(substring_index(param, '&', 1) ,3)=myTable1.n
and
myTable1.country_id=2
group by left(time,2)
)

order by hour
And the code above produces the following result.

result

(day) count
(10) 3
(10) 1
(11) 1
The following would-be code doesn't work correctly, but it will show what I want.


would-be code

(select left(time,2) as hour, count(*) as count,
sum(count) as totalCount
from myTable4
where

left(param,1)='c'
and
substring(param,3,1)=2

group by left(time,2)
)
UNION all
(select left(time,2) as hour, count(*) as count,
sum(count) as totalCount
from myTable4,myTable1

where
left(param,1)='n'
and
substring(substring_index(param, '&', 1) ,3)=myTable1.n
and
myTable1.country_id=2
group by left(time,2)
)

order by hour
And the following is my target result.

target result

(day) count totalCount
(10) 3 5
(10) 1 5
(11) 1 5


All Records From Table A - All Records From Table B - Join Alike Records
I am by no means a SQl Jedi as will be apparent by my question, but I
can usually figure out a select statement on my own. I have one today
though that really has me stumped. I am working in MySQlL 5.

In My first select statement I get all my records from Table B
SELECT
`table_A`.`ITEM`,
`table_A`.`DECSCRIPTION`,
`table_A`.`UM`,
`table_A`.`PHASE`,
`table_B`.`Qty`,
`table_B`.`Calc` as calculated
FROM
`table_A`
Inner Join `table_B` ON `req_itemlist`.`ITEM` = `table_B`.`ItemID`

In my second statement I get my records that match in this case phase
401 in Table B and all my Table A records for phase 401.

SELECT
`table_A`.`ITEM`,
`table_A`.`DECSCRIPTION`,
`table_A`.`UM`,
`table_A`.`PHASE`,
`table_B`.`Qty`,
`table_B`.`Calc` as calculated
FROM
`table_A`

Left Outer Join `table_B` ON `req_itemlist`.`ITEM` = `table_B`.`ItemID`
Where
table_A.PHASE In ('401' )

Now I need to combine the Data of both recordsets. I need EVERYTHING in
Table B, but I also need All Table A records that match the phase
selection....

Can I write this one query or do I need to use a Temp table?


Copyright © 2005-08 www.BigResource.com, All rights reserved