Copy data from table to another table MySQL - mysql

I'm having a problem updating newly added records that don't have a timestamp to another identical table in the same database. Here is my query
INSERT INTO mlscopy
SELECT * FROM mls_cvrmls AS parent
LEFT JOIN mlscopy AS child
ON child.listing_listnum != parent.listing_listnum
The parent table is updated by a separate company every morning, and unfortunately there are no timestamps(datetime) to relate the newly added records.
My child table(the copy) is needed for google geocoding since their morning udpates drop and create the parent table each morning.
I made a structure and data copy of the parent table, then deleted the last ten records to test my query. But I keep receiving the error Column count doesn't match value count at row 1.
Can't think of what I'm doing wrong here.
Here are the column table names
listing_listing
listing_listnum
listing_propertytype
listing_status
listing_listingpublicid
listing_agentname
listing_agentlist
listing_listingbrokercode
listing_officelist
listing_lo
listing_lo00
listing_lo01
listing_lo02
listing_lo03
listing_lo04
listing_lo05
listing_agentcolist
listing_agentcolist00
listing_officecolist
listing_area
listing_listdate
listing_listprice
listing_streetnumdisplay
listing_streetdirectional
listing_streetname
listing_streettype
listing_countyid
listing_zipcode
listing_zipplus4
listing_postoffice
listing_subdivision
listing_neighborhood
listing_schoolelem
listing_schooljunior
listing_schoolhigh
listing_pud
listing_lotdim
listing_acres
listing_zoning
listing_sqfttotal
listing_sqftunfinished
listing_rooms
listing_bedrooms
listing_stories
listing_basement
listing_garage
listing_garagecap
listing_fireplaces
listing_pool
listing_bathsfull
listing_bathshalf
listing_bathstotal
listing_bathsfullbsmt
listing_bathsfulllevel1
listing_bathsfulllevel2
listing_bathsfulllevel3
listing_bathshalfbsmt
listing_bathshalflevel1
listing_bathshalflevel2
listing_bathshalflevel3
listing_roombed2desc
listing_roombed2length
listing_roombed2level
listing_roombed2width
listing_roombed3desc
listing_roombed3length
listing_roombed3level
listing_roombed3width
listing_roombed4desc
listing_roombed4length
listing_roombed4level
listing_roombed4width
listing_roombed5desc
listing_roombed5length
listing_roombed5level
listing_roombed5width
listing_roomdiningdesc
listing_roomdininglength
listing_roomdininglevel
listing_roomdiningwidth
listing_roomfamilydesc
listing_roomfamilylength
listing_roomfamilylevel
listing_roomfamilywidth
listing_roomfloridadesc
listing_roomfloridalength
listing_roomfloridalevel
listing_roomfloridawidth
listing_roomfoyerdesc
listing_roomfoyerlength
listing_roomfoyerlevel
listing_roomfoyerwidth
listing_roomgreatdesc
listing_roomgreatlength
listing_roomgreatlevel
listing_roomgreatwidth
listing_roomkitchendesc
listing_roomkitchenlength
listing_roomkitchenlevel
listing_roomkitchenwidth
listing_roomlaundrydesc
listing_roomlaundrylength
listing_roomlaundrylevel
listing_roomlaundrywidth
listing_roomlivingdesc
listing_roomlivinglength
listing_roomlivinglevel
listing_roomlivingwidth
listing_roommasterbrdesc
listing_roommasterbrlength
listing_roommasterbrlevel
listing_roommasterbrwidth
listing_roomofficedesc
listing_roomofficelength
listing_roomofficelevel
listing_roomofficewidth
listing_roomother1desc
listing_roomother1length
listing_roomother1level
listing_roomother1width
listing_roomother1
listing_roomother2desc
listing_roomother2length
listing_roomother2level
listing_roomother2width
listing_roomother2
listing_roomrecdesc
listing_roomreclength
listing_roomreclevel
listing_roomrecwidth
listing_handicap
listing_yearbuilt
listing_lotdesc
listing_construction
listing_watertype
listing_roof
listing_attic
listing_style
listing_floors
listing_fireplacedesc
listing_structure
listing_walltype
listing_basedesc
listing_appliances
listing_interior
listing_exterior
listing_amenities
listing_pooldesc
listing_fence
listing_porch
listing_heatsrc
listing_heatsystem
listing_coolsystem
listing_waterheater
listing_watersewer
listing_parking
listing_garagedesc
listing_handicapdesc
listing_feedesc
listing_restrictions
listing_terms
listing_assocfeeincludes
listing_building
listing_possession
listing_farmtype
listing_ownerdesc
listing_irrigationsrc
listing_taxyear
listing_taxamount
listing_directions
listing_remarks
listing_virtualtourlink
listing_vowavmyn
listing_vowcommyn
listing_addressdisplayyn
listing_f174
listing_proptype
listing_lat
listing_lon
listing_photo1
listing_listofficename
listing_vtoururl
listing_multiphotoflag
id <- primary key

If you only run the SELECT statement from your INSERT you will see that your select returns all the columns of both mls_cvrmls AND mlscopy.
You probably need:
INSERT INTO mlscopy
SELECT parent.* FROM mls_cvrmls AS parent
LEFT JOIN mlscopy AS child
ON child.listing_listnum != parent.listing_listnum
EDIT
I am not sure your JOIN condition is correct. This kind of condition will probably return many records you did not wish for. Each record in mls_cvrmls has many (many!) records in mlscopy which satisfy the condition.
As an example, let's assume the 2 tables have 3 columns, and you want to add all records from parent to child, as long as they don't exists there anymore.
INSERT INTO mlscopy (listing_listing, listing_listnum, listing_propertytype)
SELECT parent.listing_listing,
parent.listing_listnum,
parent.listing_propertytype // (more columns...)
FROM mls_cvrmls AS parent
LEFT JOIN mlscopy AS child
ON child.listing_listnum = parent.listing_listnum
WHERE child.listing_listnum IS NULL

Couple of things here.
The error message is because "select *" gives you all columns from all tables in the query. That is, each row has all the columns from mls_cvrmls PLUS all the columns from mlscopy. This is not going to be suitable for inserting into mlscopy because it's going to have many extra columns. If the two tables have all the same columns, then they will all be doubled.
Your WHERE clause is unlikely to be correct. This is saying that for every row in parent, you want all the rows in child that don't match. Think this through. Suppose parent has listing_listnum values of 1, 2, and 4, and child has value 1, 4, and 5. So the pairs 1/1 and 4/4 will be excluded. But you'll get the pairs 1/4, 1/5, 2/4, 2/5, 4/1, and 4/5. I think what you really want here is to just get the records from parent that aren't found on child at all, like in this example, just 2. So what you probably really want is a "not exists" query.
I'm not entirely clear from your description, but you say you want to "update newly added records", but then you do an INSERT. Do you want to update existing records, or do you want to insert new records?
So assuming that what you want to do is find records that are in mls_cvrmls but not in mlscopy and insert these records, I think the correct query would be more like -- and your field list is long so I'll just pick a few sample fields to make the point:
insert into mlscopy (listing_listing, listing_listnum, listing_propertytype, listing_status
listing_listingpublicid, listing_agentname)
select listing_listing, listing_listnum, listing_propertytype, listing_status
listing_listingpublicid, listing_agentname
from mls_cvrmls
where not exists (select 1 from mlscopy where mlscopy.listing_listnum=mls_cvrmls.listing_listnum)
As Icarus says, you should list all columns explicitly. Among the many reasons for this, even if the two tables have all the same fields, if they do not occur in the same order, "insert into mlscopy select *" will not work, because a SQL engine does not match names, it just takes the fields in each table in the order they occur. This may seem like a pain if the list is long, but trust me, after you've been burned a few times by mysterious problems, you'll want to list the fields explicitly.
And just a side note: Why do you prefix all the column names with "listing_" ? This just makes more to type every time you use the table. If you have another table that has names that would otherwise be the same and you need to distinguish, you can always prefix with the table name, like "mls_cvrmls.propertytype".

Get used to list-all-your-columns and you will save yourself some headaches like this and in the future your code won't break if they add more columns.
Change your sql statement to something like this
INSERT INTO mlscopy (col1,col2,col3...coln)
SELECT col1,col2,col3....coln FROM mls_cvrmls AS parent
LEFT JOIN mlscopy AS child
ON child.listing_listnum != parent.listing_listnum

The two tables have different structures, and you're not specifying WHICH fields would be copied across. If you must have different structures, you'll have to explicitly state WHICH fields should be copied. MySQL isn't smart enough to figure that sort of mismatch out on its own, so it complains and aborts.

Related

MySQL how to select on multiple tables using Not Exist

I have three tables. One is a table of deletion candidates. This table was created with certain criteria, but did not include a couple of factors for consideration (limitations of the system). The other two tables were created considering those "left out" factors. So, I need to run a SELECT query on these three tables to come up with a deletion list.
What I started with is:
SELECT inactive.id
FROM inactive, renamed, returned
WHERE NOT EXISTS (inactive.id = remamed.id and inactive.id = returned.id)
But this is giving me an error. Can someone point out my error here?
Thank you
It's not entirely clear what you are trying to do here.
I assume you want a list of all rows from the inactive table that do not exist in either the renamed table or the inactive table. Is that right?
If so you can use a query like this:
SELECT inactive.id
FROM inactive
WHERE NOT EXISTS (select null from renamed where renamed.id = inactive.id)
AND NOT EXISTS (select null from returned where returned.id = inactive.id)

Find matches from 2 tables, change other field?

I have a database with two separate tables. One table (T1) has 400+ values in its only column, while the other (T2) has 14,000+ rows and multiple columns.
What I need to do is to compare the column in T1 to one column in T2. For every matching value, I need to update a different value in the same row in T2.
I know this is pretty easy and straight-forward, but I'm new to MySQL and trying to get this down before I go back to other things. Thanks a ton in advance!
EDIT: Here's what I've been trying to no avail..
UPDATE `apollo`.`Source`, `apollo`.`Bottom`
SET `Source`.`CaptureInterval` = '12'
WHERE `Bottom`.`URL` LIKE `Source`.`SourceID`
EDIT 2:
A little clarification:
apollo.Bottom and apollo.Source are the two tables.
apollo.Bottom is the table with one column and 400 records in that column.
I want to compare Bottom.URL to Source.SourceID. If they match, I want to update Source.CaptureInterval to 12.
You can use the following query to update. But the performance will be much better if you index URL and SourceID columns in both tables as they are being used in the WHERE clause.
UPDATE `apollo`.`Source`, `apollo`.`Bottom`
SET `Source`.`CaptureInterval` = '12'
WHERE `Bottom`.`URL` = `Source`.`SourceID`
You can join the two tables together and do a multiple table update.
Start with something like this:
UPDATE `apollo`.`Source`
INNER JOIN `apollo`.`Bottom` ON `apollo`.`Bottom`.`URL` = `apollo`.`Source`.`SourceID`
SET `apollo`.`Source`.`CaptureInterval` = '12';

Join TableA, TableB and TableC to get data from TableA and TableC

Why I am getting so many records for this
SELECT e.OneColumn, fb.OtherColumn
FROM dbo.TABLEA FB
INNER JOIN dbo.TABLEB eo ON Fb.Primary = eo.foregin
INNER JOIN dbo.TABLEC e ON eo.Primary =e.Foreign
WHERE FB.SomeOtherColumn = 0
When I am running this I am getting Millions of records which is not the correct case, all tables has less number of records.
I need to get the columns from TableA and TableC and because they are not joined logically so I have to use TableB to act as bridge
EDIT
Below is the count:
TABLEA = 273551
TABLEB = 384412
TABKEC = 13046
Above Query = After 2 minutes I have forcefully canceled the query.. till that time the count was 11437613
Any suggestion?
To figure out what is going on in such a query where the results are not as expected, I tend to do this. First I change to a SELECT * (Note this is only for figuring out the problem, do not use SELECT * on production, ever!) Then I add an order by for the ID frield from tableA if there is not one in the query.
So now I run the query up to the first table including any where conditions that are from the first table. I comment out the rest. I note the number of records returned.
Now I add in the second table and any where conditions from it. If I am expecting a one to relationship, and if this query doesn't return the smae number of records, then I look at the data that is being returned to see if I can figure out why. Since the contents are ordered by the table1 ID, you can ususally see examples of some records that are duplicated fairly easily and then scroll over until you find the field that causes the differnce. Often this means that you need some sort of addtional where clause or aggregation on the fields in the next table to limit to only one record. JUSt note down the problem at this point though as you may be able tomake the change more effectively in the next join.
So add inteh the third table and again, not the number of records and then look closely at the data where the id from A is repeated. LOok at the columns you intend to return, are they always teh same for an id? If they are differnt then you do not havea one-one relationship and you need to understand that either theri is a data integrity problem or you are mistaken in thinking there is a one-to-one. If tehy are the same, then a derived table may be in order. You only need the ids from tableb so the join could look something like this:
JOIN (SELECT MIn(Primary), foreign FROM TABLEB GROUP BY foreign) EO ON Fb.Primary = eo.foreign
Hope this helps.

Use of inner join seems to cause entries in a #temp table to disappear

I am brand new to SQL and am working with the following code provided to us by one of our vendors:
SELECT DISTINCT MriPatients.PatientID
INTO #UniquePt
FROM MriPatients
INNER JOIN #TotalPopulation ON MriPatients.PatientID = #TotalPopulation.PatientID
Set #TotalUniquePatients = (Select Count(*) FROM #UniquePt)
What happens is the Set line causes #TotalUniquePatients to be set to 0 even though there are many unique patient ids in our database. That value is then later used as a denominator in a division which causes a divide by 0 error.
Now it seems to me that this is easy to fix by using COUNT DISTINCT on the MriPatients table; then you don't need to create #UniquePt at all...this is the only place that table is used. But, I don't understand why the code as it is gets a 0 result when counting #UniquePt. If you remove the INNER JOIN, the Set returns a correct result...so what does the INNER JOIN do to #UniquePt?
If it matters, we are using SQL Server 2008.
The result is 0 because of 1 of 2 situations:
#TotalPopulation is empty
#TotalPopulation contains no records that have the same value for PatientID as the records in MriPatients
How are you populating #TotalPopulation?
A COUNT DISTINCT won't necessarily do the same thing. It depends on what you fill #TotalPopulation with. If all you want is the number of unique patients in MriPatients then yes, the COUNT DISTINCT will work. But if you're filling #TotalPopulation based on some kind of logic then they're the COUNT DISTINCT won't necessarily give you the same results as the COUNT of the joined tables.
The INNER JOIN causes you to insert ONLY records that have a matching PatientID in the #TotalPopulation table.
I'm guessing you don't, or that table isn't populated, which is causing the issue.
Is there a reason you are joining to it in the first place?

Record-doubling problem on a simple left join

I'm running this query:
CREATE TABLE
SELECT people.*, Sheet1.department
FROM people LEFT JOIN Sheet1 ON people.depno = Sheet1.depno
On a set of tables detailing employee records.
The goal is to create a new table that has all the "people" data, plus a human-readable department name. Simple, right?
The problem is that each record in the resulting table appears to be duplicated exactly (with literally every field being the same), turning a roughly 23,000-record table into a roughly 46,000-record table. I say "roughly" because it's not an exact doubling -- there's a difference of about a hundred records.
Some details: The "people" table contains 15 fields, including the "depno" field, which is an integer indicating department.
The "Sheet1" table is, as one would guess, a table generated from an imported xls file containing two fields: the shared "depno" and a new "department" (the latter being a verbose department name corresponding to the depno in question). There are 44 records in the "Sheet1" table.
Thanks in advance for any pointers on this. Let me know what other information you can use from me.
Update: Here's the code I ended up using, from my response to Johan (thanks again to everyone who worked on this):
CREATE TABLE morebetter
SELECT people.*, Sheet1.department FROM people
LEFT JOIN Sheet1 ON people.depno = Sheet1.depno
GROUP BY id
Sounds like the Sheet1.depno field isn't unique?
The people.depno is not unique, that's why you're getting the doubling.
Change the SELECT part to
SELECT DISTINCT people.*, Sheet1.department
FROM people LEFT JOIN Sheet1 ON people.depno = Sheet1.depno
This will eliminate duplicate rows.
In MySQL you can also write
SELECT people.*, Sheet1.department
FROM people LEFT JOIN Sheet1 ON people.depno = Sheet1.depno
GROUP BY people.depno
Which works slightly different.
The first query eliminates rows with duplicate output, the second query eliminates records with duplicate people.depno, even if people.depno does not appear in the output.
I like the second form, because it makes explicit which duplicate you're trying to eliminate and you don't need to tweak the output.
It's also slightly faster in executing time.
***Warning***
The group by version will eliminate any double people.depno it finds, but if the other fields in the select are not identical it will just choose one at random!
In other words. If the outcome of the select distinct is different from the group by version that means that MySQL is silently dropping non-duplicate rows.
This may or may not be what you want!
In order to be safe, do a group by on all fields that you care about!
If the group by is on a unique key than it's pointless to include further fields from the same table as that unique key.