I have an existing sql database which has 2 columns of data (part and variant), another one of our systems uses sql in a different table in the same database but columns are (part and type), how would i merge the two in a datagridview (part, variant and type) using the part to do the select statement in a way to pull the other two bits of info and include them in the same datagridview. In vb.net
Cheers,
Pete
My guess would be that you would need an outer join to get the data, assuming there is an unique key and it's "part", for example:
Select part, variant, type
from table1,table2
where table1.part = table2.part;
Assuming that each table is unique and there is no relationship to part, nor a key:
Select part, variant
from table1
UNION ALL
select part, type
from table2;
This is ANSI SQL, so you may have to adapt it to SQL Server syntax, but I think the intent is clear.
Bind the resultset to the DataGridView.
Does this answer you question?
R/
Prescott ....
Related
I have a huge table of mysqlwhich contains more than 33 million records .How I could compare my table to found non duplicate records , but unfortunately select statement doesn't work. Because it's huge table.
Please provide me a solution
First, Create a snapshot of your database or the tables you want to compare.
Optionally you can also limit the range of data you want to compare , for example only 3 years of data. This way your select query won't hog all the resources.
Snapshot will be bunch of files each representing a table containg your primary key or business key for each record ( I am assuming you can compare data based on aforementioned key . If thats not the case record all the field in your file)
Next, read each records from the file and do a select against the corresponding table. If there are more than 1 record you know it is a duplicate
Thanks
Look at the explain plan and see if what the DB is actually doing for the NOT IN.
You could try refactoring, with an index on subscriber as Roy suggested if necessary. I'm not familiar enough with MySQL to know whether the optimizer will execute these identically.
SELECT *
FROM contracts
WHERE NOT EXISTS
( SELECT 1
FROM edms
WHERE edms.subscriber=contracts.subscriber
);
-- or
SELECT C.*
FROM contracts AS C
LEFT
JOIN edms AS E
ON E.subscriber = C.subscriber
WHERE E.subscriber IS NULL;
We have an old FoxPro DB that still has active data being entered into it. I am in the process of writing a series of .bat files that will update a MySQL database for our web applications that I'm working on.
Our FoxPro databases were never set up with unique IDs or anything useful like that so I'm having to have the query look at a few different fields.
Here's my query thus far:
//traininghistory = MySQL DB
//traininghistory_test = FoxPro DB
INSERT INTO traininghistory
WHERE traininghistory_test.CLASSID NOT IN(SELECT CLASSID FROM traininghistory)
AND traininghistory_test.EMPID NOT IN(SELECT EMPID FROM traininghistory)
What I'm After is this:
I need an query that looks at the 600,000+ entries in the FoxPro DB (traininghistory_test in my code) and compares to the 600,000+ entries in the MySQL DB (traininghistory in my code) and only inserts the ones where the columns CLASSID and EMPID are new- that is, they are NOT in the traininghistory table.
Any thoughts on this (or if you know a simpler/more efficient way to execute this query in MySQL) are greatly appreciated.
One option is to use a outer join / null check:
insert into traininghistory
select values
from traininghistory_test tht
left join traininghistory th on tht.empid = th.empid
and tht.classid = th.classid
where th.empid is null
It's also worth noting, your current query may leave out records since it's not comparing empid and classid in the same records.
One way ist.
CREATE ONE UNIQUE INDEX ON THE COLUMS (CLASSID, EMPID),
THEN
INSERT IGNORE INTO traininghistory SELECT * or fieldlist FROM traininghistory_test;
Thats all
I'm trying to build an updatable view in Access for a user. Basically, the underlying tables look like this:
Accounts
--------
accountId
accountName
accountHolder
TransactionStatements
------------
statementId
accountId
received
month
year
The user wants a Query (view) that looks like this:
StatementView
-------------
accountName
accountHolder
year
janReceived
febReceived
marReceived
etc...
The SQL to accomplish this is straightforward using a transpose, but the resulting view is not updatable.
I've also tried doing multiple joins explicitly to accomplish this:
PARAMETERS [Enter Year:] Long;
SELECT accountName, accountHolder, year,
FROM ((Accounts a
INNER JOIN TransactionStatements ts1 ON a.accountID = ts.accountID) 'AND month = 1 (This isn't allowed for some reason?)
INNER JOIN TransactionStatements ts2 ON a.accountID = ts.accountID) 'AND month = 2 (This isn't allowed for some reason?)
WHERE ts1.month = 1 AND ts2.month = 2 AND ts1.year = ([Enter Year:]) AND ts2.year = ([Enter Year:])
But once again the result becomes non-updatable as soon as I add the second INNER JOIN. I've looked at this MS help page, but it hasn't helped me figure out the right way to do this.
It suggests Forms as an alternative, but building a custom form in Access appears to be an even more arcane and convoluted process than writing views.
Any suggestions?
Take a look at this very thorough list of reasons why recordsets are and aren't updateable:
When Recordsets Are Always Updateable
A recordset is always updateable when:
It is based on a single table.
It is based on a query based on a single table.
It is based on a query based on tables with a one-to-one relationship.
When Recordsets Are Never Updateable
A recordset is never updateable when:
It is based on a Crosstab query.
It is based on a Union Query.
It is an Aggregate Query that calculates a sum, average, count or other type of total on the values in a field.
It is an Update Query that references a field in the Update To row from either a crosstab query, select query, or subquery that
contains totals or aggregate functions
Note: By using a domain aggregate function in the Update To row of an update query, you can reference fields from either a crosstab query, select query, or subquery that contains totals or aggregate functions.
It is based on a Query that includes a linked ODBC table with no unique index.
The database was opened as read-only or is located on a read-only drive.
It is a SQL pass-through query.
It is a query whose UniqueValues property is set to Yes. (That is, it is a query with a DISTINCT predicate.)
Cartesian Joins (that is, a query that includes more than one table or query, and the tables or queries aren't joined by a join
line in Design view.)
Query based on three or more tables in which there is a many-to-one-to-many relationship. Note: Though you can't update
the data in the query directly, you can update the data in a form
or data access page based on the query if the form's RecordsetType
property is set to Dynaset (Inconsistent Updates).
Calculated fields. Even if the query itself is updateable, if a column in a query is based on a formula, the field cannot be
updated. However, if the other fields in the formula are updated,
the calculated field will automatically update.
Recordsets Are Updateable Under Certain Conditions
Some queries, especially those involved in a Join, will not be updateable under some conditions, but will be under others. In other queries, even if the query itself is updateable, some of the fields will not be. The following are cases of query problems and their corresponding solutions.
Query based on a Join of tables with no Relationship.
•Problem: If a query is based on two or more tables that DO NOT have a relationship established (with Referential Integrity enabled), the query will be non-updateable.
•Solution: Create a Primary Key or Unique Index on ALL of the fields used in the Join on the "one-side" table. To be clear, this means ONE primary key or unique index based on all of the fields, not separate indexes on each field.
In a query based on a Join of tables with a one-to-many relationship (1:M), you might not be able to edit the data in one or more fields.
Join field from the "one" side
•Problem: If you have a 1:M relationship created between two tables, you cannot change the primary key field (used in the Join) of the table on the "one" side of the relationship.
•Solution: Enable cascading updates between the two tables.
New records, if the "many" side join field doesn't appear in the datasheet
•Problem: In a query based on a 1:M relationship, you can create a new record and fill in the fields that come from the "one" side table, but if the join field from the "many" side table is not visible in the query (that is, the foreign key), you cannot add data to the "many" side fields.
•Solution: Add the join field from the "many" side table (ie, foreign key) to your query to allow adding new records.
New records on the "one" side that are duplicates of other "one" side records.
•Problem: When adding a new record, if you try to type into the "one" side fields, you will be attempting to create a new record. Even if you use the same primary key values, it will give you an error.
•Solution: Add a value to the "many" side join field (foreign key) that matches the "one" side join field (primary key) of an already existing record. The "one" side values will simply appear.
Join field from the "many" side, after you've updated data on the "one" side
•Problem: If you are currently editing fields from the "one" side of the relationship, you cannot change the "many" side join field (foreign key).
•Solution: Save the record; then you'll be able to make changes to the "many" side join field.
New records, if entire unique key of ODBC table isn't output
•Problem: This is different than #5 under Never Updateable. In this case, the primary key of the linked ODBC table exists, but is not added to the query.
•Solution: Select all primary key fields of ODBC tables to allow inserts into them.
Query does not have Update Data permissions
•Problem: Query (or underlying table) for which Update Data permission isn't granted.
•Solution: To modify data, permissions must be assigned.
Query does not have Delete Data Permissions
•Problem: Query (or underlying table) for which Delete Data permission isn't granted
•Solution: To delete data, permissions must be assigned.
Conclusion
The causes of non-updateable recordsets are many and varied. Some have solutions and others don't. Hopefully, this list will help you know the difference.
The above is taken, word for word, from the following blog: This Recordset Is Not Updateable. Why?
I felt it was best to copy and fully attribute rather than try to paraphrase, so that all of the information was given.
The first things I suggest you look at might be that your tables have proper indexing and their relationships are set up properly. Those are usually the two things I gun for first, and they tend to solve most of my own "non-updateable query" issues.
I am working with BDS 2006,MySQL DB (MyDAC components used for connection) and i have a DBGrid component on my form which displays the records from my DB table.
Now i need to JOIN two tables and display the results in my DBGrid
The Resulting view that I should get is the result of the query
SELECT e_salary.e_basic,e_details.e_name
FROM e_details
INNER JOIN e_salary
ON e_details.e_id=e_salary.e_id;
there is one more option to do it as I searched
SELECT
e_salary.e_basic,e_details.e_name
FROM
e_details, e_salary
WHERE
e_details.e_id=e_salary.e_id;
e_details,e_salary are my two tables and e_id is my PRIMARY KEY
Presently I am having 2 DBGrid one is for e_details and other for e_salary
Is it possible to have only 1 DBGrid displaying values from both the Tables? or I have to display 2 separate DBGrid?
If possible then how do I go about it
P.S.- there are more columns to be added in the view and both tables have same no of rows
Thanks in advance
DBGrid displays a dataset data. The data may be result of some SQL query execution. DBGrid, TDataSet and TDataSource do not cary what was the SQL query. Single table SELECT, multi table SELECT with joins, stored procedure call or SHOW command. So, yes - you can use 1 DBGrid to display resultset of your SELECT joining 2 tables.
If both tables have the same number of rows, e_id is primary key for both tables, then why not to have single table, containing columns of both tables ? Also, if you will need to edit your dataset data, then there may be problems to update columns of both tables. And that may be one more argument to have single table.
Although you can use WHERE e_details.e_id=e_salary.e_id instead of JOIN e_salary ON e_details.e_id=e_salary.e_id. The JOIN is preferred, because DBMS gets your intent more explicitly and that is more readable for others.
The DBgrid is probably not the component you need.
Give an eye to the TTreeView
http://delphi.about.com/od/vclusing/l/aa060603a.htm
I got a table with a normal setup of auto inc. ids. Some of the rows have been deleted so the ID list could look something like this:
(1, 2, 3, 5, 8, ...)
Then, from another source (Edit: Another source = NOT in a database) I have this array:
(1, 3, 4, 5, 7, 8)
I'm looking for a query I can use on the database to get the list of ID:s NOT in the table from the array I have. Which would be:
(4, 7)
Does such exist? My solution right now is either creating a temporary table so the command "WHERE table.id IS NULL" works, or probably worse, using the PHP function array_diff to see what's missing after having retrieved all the ids from table.
Since the list of ids are closing in on millions or rows I'm eager to find the best solution.
Thank you!
/Thomas
Edit 2:
My main application is a rather easy table which is populated by a lot of rows. This application is administrated using a browser and I'm using PHP as the intepreter for the code.
Everything in this table is to be exported to another system (which is 3rd party product) and there's yet no way of doing this besides manually using the import function in that program. There's also possible to insert new rows in the other system, although the agreed routing is to never ever do this.
The problem is then that my system cannot be 100 % sure that the user did everything correct from when he/she pressed the "export" key. Or, that no rows has ever been created in the other system.
From the other system I can get a CSV-file out where all the rows that system has. So, by comparing the CSV file and my table I can see if:
* There are any rows missing in the other system that should have been imported
* If someone has created rows in the other system
The problem isn't "solving it". It's making the best solution to is since there are so much data in the rows.
Thanks again!
/Thomas
We can use MYSQL not in option.
SELECT id
FROM table_one
WHERE id NOT IN ( SELECT id FROM table_two )
Edited
If you are getting the source from a csv file then you can simply have to put these values directly like:
I am assuming that the CSV are like 1,2,3,...,n
SELECT id
FROM table_one
WHERE id NOT IN ( 1,2,3,...,n );
EDIT 2
Or If you want to select the other way around then you can use mysqlimport to import data in temporary table in MySQL Database and retrieve the result and delete the table.
Like:
Create table
CREATE TABLE my_temp_table(
ids INT,
);
load .csv file
LOAD DATA LOCAL INFILE 'yourIDs.csv' INTO TABLE my_temp_table
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(ids);
Selecting records
SELECT ids FROM my_temp_table
WHERE ids NOT IN ( SELECT id FROM table_one )
dropping table
DROP TABLE IF EXISTS my_temp_table
What about using a left join ; something like this :
select second_table.id
from second_table
left join first_table on first_table.id = second_table.id
where first_table.is is null
You could also go with a sub-query ; depending on the situation, it might, or might not, be faster, though :
select second_table.id
from second_table
where second_table.id not in (
select first_table.id
from first_table
)
Or with a not exists :
select second_table.id
from second_table
where not exists (
select 1
from first_table
where first_table.id = second_table.id
)
The function you are looking for is NOT IN (an alias for <> ALL)
The MYSQL documentation:
http://dev.mysql.com/doc/refman/5.0/en/all-subqueries.html
An Example of its use:
http://www.roseindia.net/sql/mysql-example/not-in.shtml
Enjoy!
The problem is that T1 could have a million rows or ten million rows, and that number could change, so you don't know how many rows your comparison table, T2, the one that has no gaps, should have, for doing a WHERE NOT EXISTS or a LEFT JOIN testing for NULL.
But the question is, why do you care if there are missing values? I submit that, when an application is properly architected, it should not matter if there are gaps in an autoincrementing key sequence. Even an application where gaps do matter, such as a check-register, should not be using an autoincrenting primary key as a synonym for the check number.
Care to elaborate on your application requirement?
OK, I've read your edits/elaboration. Syncrhonizing two databases where the second is not supposed to insert any new rows, but might do so, sounds like a problem waiting to happen.
Neither approach suggested above (WHERE NOT EXISTS or LEFT JOIN) is air-tight and neither is a way to guarantee logical integrity between the two systems. They will not let you know which system created a row in situations where both tables contain a row with the same id. You're focusing on gaps now, but another problem is duplicate ids.
For example, if both tables have a row with id 13887, you cannot assume that database1 created the row. It could have been inserted into database2, and then database1 could insert a new row using that same id. You would have to compare all column values to ascertain that the rows are the same or not.
I'd suggest therefore that you also explore GUID as a replacement for autoincrementing integers. You cannot prevent database2 from inserting rows, but at least with GUIDs you won't run into a problem where the second database has inserted a row and assigned it a primary key value that your first database might also use, resulting in two different rows with the same id. CreationDateTime and LastUpdateDateTime columns would also be useful.
However, a proper solution, if it is available to you, is to maintain just one database and give users remote access to it, for example, via a web interface. That would eliminate the mess and complication of replication/synchronization issues.
If a remote-access web-interface is not feasible, perhaps you could make one of the databases read-only? Or does database2 have to make updates to the rows? Perhaps you could deny insert privilege? What database engine are you using?
I have the same problem: I have a list of values from the user, and I want to find the subset that does not exist in anther table. I did it in oracle by building a pseudo-table in the select statement Here's a way to do it in Oracle. Try it in MySQL without the "from dual":
-- find ids from user (1,2,3) that *don't* exist in my person table
-- build a pseudo table and join it with my person table
select pseudo.id from (
select '1' as id from dual
union select '2' as id from dual
union select '3' as id from dual
) pseudo
left join person
on person.person_id = pseudo.id
where person.person_id is null