I fairly new to MYSQL. I have 3 tables
Table.... accounts
ROWS.... id : rp_total
Table... research
ROWS.... research_uid : cost
Table... completed_research
ROWS.... id : research_uid
I'm trying to run a script automatically that will look for id's that don't have all completed research and will insert a new row into the completed_research table with their ID and the completed research_uid if the rp_total amount is greater than the research_uid cost
This is hard to explain. Hopefully this makes sense. I'm trying to avoid looping through id's because there a potential for thousands to be updated at once. I'm guessing this is going to required a view or temporary table of some sort but I've been fighting with this for 2 solid days now.
It's not exactly what you want, but must be close:
INSERT
INTO
completed_research(a_id, research_uid)
SELECT
a.id,
r.research_uid
FROM
accounts a,
research r
WHERE
(
a.INCOMPLETED_FIELD IS NULL AND a.id > r.research_uid
);
"a.INCOMPLETED_FIELD" is your incomplete column in the accounts table, I not sure what are suppose to be, but I imagine that it could help you.
*Note that INSERT to "a_id" and not "ID" column, for good practices.
Related
Sorry for the bad description, not sure what to call this.
I'm trying to migrate a bad database structure to a better one.
The old storage, lets say customer name in say invoice table.
The new I going to need a ref to customer-table so that more information about the customer can be queried if needed.
So, to not completely break all the old entries, I would like a query that take col2,col3,col4 from tbl1 and and queries tbl2,tbl3 and tbl4 to get and ID of
each of the content of the columns.
Then I would want to update the row in tbl1 with these ID's in their respectable column.
The thing is, I have no idea about how to do this.
I don't even know what to search for to learn about it.
Better example:
Imagen you have pet-owner table. The old table had:
Owner: John
pet: Dog
But now I have a table for owners and a table for pets, so I want to update the columns in pet-owner table so for each row it will (in this case) search ower-table for 'John' and pet-table for 'dog' and return their ID and update the pet-owner table with that.
You'll have a much easier time if you add new columns to hold the ids, populate them, and then drop the old columns later.
If you do that, your update would be like:
update pet_owner
inner join pet on pet_owner.pet_name = pet.name
inner join owner on pet_owner.owner_name = owner.name
set pet_owner.pet_id = pet.id
pet_owner.owner_id = owner.id
;
New to MySQL, searched for answers, but multiple database questions seem to be all about combining tables, that's not what I'm after.
In new database, I duplicated a table from old database, with most columns but not all.
I need to get customer number from old database where customer name has 'Co.' in their name. This should return 14 or so customers with about 80 rows
I then need to delete all orders in the second database that has those customer numbers.
Is this possible with a subquery? that's where I am stuck right now.
Thanks so much.
You can work with multiple databases on one query:
Ex:
SELECT database1.col1, database2.col2
FROM database1.options, database2.options
WHERE database1.option_name="sort_order"
Say you have a table of SO threads (threads_table) from which you need to eliminate duplicates that you have already identified in the Problem field of another table (problem_log).
DELETE FROM `threads_table`
WHERE `thread_ID` IN
(SELECT `Thread_ID` from `problem_log_table`
WHERE `Problem`='Duplicate');
Edited to add:
Here's one way to do it, if I'm understanding your needs correctly. (Btw, I've assumed away the added complexity of working with tables it two different databases.)
DELETE FROM tbl2
WHERE tbl2.customer_num IN
(SELECT tbl1.customer_num from `tbl1`
WHERE tbl1.customer_name LIKE '%Co.%');
I have two tables that have different data that I need to merge. They do have similarities such as: Order number, Name, type or product. But they have separate data as well like: Order date, and Engravings.
Would I do two separate Append queries in Access into a merged table? Or one Append queries? Or just keep the data separate?
I am new to Access and trying to find the best way to approach this.
Merging the two tables into one completely defeats the purpose of using a database and you're better off using excel at that point. You want to split the data as much as possible along logical lines so that you can find, say... all the orders that Mr X has ever made for a specific product. And in that case you're going to want to have separate tables for customers, orders, engravings and the like.
The best practice from a design standpoint is to place fields that each table has in common into a third "master" table, then create relationships from that table to the existing tables and delete the data that has been transferred to the main table (except for the primary keys, which have to be common with your master table).
To create the master table, use a Make Table query to generate the master table based on one of your tables, then an append query to add any products in the master table that might not be common to both, based on the other table. Finally, delete queries for each table would rid you of redundant data in both original tables.
However, I strongly suggest you use Microsoft's tutorials and download the NorthWind sample database so you can get an idea of what a properly structured database looks like. The beginner's learning curve for access is very steep and having well built example databases is almost a requisite.
Make a backup of your database(s) and play with it until it turns out right. Do not make the mistake of playing with live data until you know what you're doing.
As you have similar fields on either table, take the Order number field from both tables using a union query. Something like:
SELECT tbl_Delivery_Details.OrderNo
FROM tbl_Delivery_Details
GROUP BY tbl_Delivery_Details.OrderNo
UNION
SELECT tbl_Delivery_Header.[Order number]
FROM tbl_Delivery_Header
GROUP BY tbl_Delivery_Header.[Order number];
This would take the order numbers from the delivery details table and from the delivery header table and merge them into one list with only one instance of each order number. Save the query.
You could then use this query in a new query. Bring in your 2 tables to this query and insert the fields from either table that you require.
As users add records to the tables they will be added to the union selet query when it is next run.
PB
It depends on what you want to do. Let's assume you have tables A (with 50 records) and B (with 75) records, and both tables have a similar column called OrderID.
Appending Rows:
If you want to create a table with 125 total records by combining records (rows) from A and records (rows) from B, run the following two queries:
Query 1:
SELECT A.ORDER_NUMBER, A.TEXT_FIELD1 as DATA INTO C
FROM A;
Query 2:
INSERT INTO C ( ORDER_NUMBER, DATA )
SELECT B.ORDER_NUMBER, B.TEXT_FIELD2
FROM B;
Appending Columns: If you want to create a table with 75 total records where you are appending columns from A to the columns in B, then run the following query:
SELECT B.ORDER_NUMBER, A.TEXT_FIELD1, B.TEXT_FIELD2 INTO C
FROM A RIGHT JOIN B ON A.ORDER_NUMBER = B.ORDER_NUMBER;
... in a similar way, you can append columns in B to columns in A in a new table C with a total of 50 records by running the following query:
SELECT A.ORDER_NUMBER, A.TEXT_FIELD1, B.TEXT_FIELD2 INTO C
FROM A LEFT JOIN B ON A.ORDER_NUMBER = B.ORDER_NUMBER;
Sorry if the titles abit wrong been stuck on this for a while now and my brain is well and truely frazzled at this point..
I've simplied the question to the basics. There are 2 tables..
TABLE A
------------
-id
TABLE B
-------------
-table_a_fk
-user_id
I'm trying to find the ID of Table A using a list of user_ids... and only if the entry exists in Table B for all users..
So if the user_ids were 10,11,12 the query would only return a result if Table B had rows for users 10,11,12 with the same table_a_fk. It's quite important too that if there is extra entries (i.e users 10,11,12,13,14) with the same table_a_fk in Table B then this does not return.. it needs to be an exact match.
I'm starting to think what I need is impossible just in MYSQL and I'll need to process the rows with a more general search... would love for one of you SQL gurus to prove me wrong though ;)
Many thanks in advance for an help and suggestions
UPDATE: Just realiased that table A might not even be needed in this query... as if the user_ids all match then the value of table_a_fk would be the same as taking it from Table A.
Like I said my brains not working atm aha... thanks for any help!
This is an example of a set-with-set query. I like to handle this using group by and having:
select table_a_fk
from b
group by table_a_fk
having sum( b.user_id in (10, 11, 12) ) = 3 and
count(*) = 3;
You need to adjust the in list and the 3 to match the list of users you care about and the number of such users. This assumes that there are no duplicates in the table.
I have two tables: all_users and vip_users
all_users table has a list of all users (you don't say?) in my system and it currently has around 57k records, while vip_users table has around 37k records.
Primary key in both tables is an autoincrement id field. all_users table is big in terms of attribute count (around 20, one of them is email), while vip_users table has only (along with id) email attribute.
I wanted to query out the "nonVip" users by doing this (with help of this question here on SO):
SELECT all_users.id, all_users.email
FROM all_users
LEFT OUTER JOIN vip_users
ON (all_users.email=vip_users.email)
WHERE vip_users.email IS NULL
And now, finally coming to the problem - I ran this query in phpmyadmin and even after 20 minutes I was forced to close it and restart httpd service as it was taking too long to complete, my server load jumped over 2 and the site (which also queries the database) became useless as it was just loading too slow. So, my question is - how do I make this query? Do I make some script and run it over night - not using phpmyadmin (is this maybe where the problem lies?), or do I need to use different SQL query?
Please help with your thoughts on this.
Try indexing the fields email on both tables, that should speed up the query
CREATE INDEX useremail ON all_users(email)
CREATE INDEX vipemail ON vip_users(email)
As written, you're not getting the results you're looking for. You're looking for vip_users rows where the email matches an email in users, and is also NULL.
Is there a reason you want vip_users to have a separate id from users? If you change the vip_users id field to a fk on the users id field, yo would then change your select to:
SELECT all_users.id, all_users.email
FROM all_users
LEFT OUTER JOIN vip_users
ON (all_users.id=vip_users.id)
WHERE vip_users.email IS NULL;
There's no reason this query should take any discernible about of time. 37k records is not a very big table....
I think NOT IN is faster and used less resource than LEFT OUTER JOIN.
Can you try -
SELECT *
FROM all_users
WHERE id NOT IN (SELECT id
FROM vip_users
WHERE email IS NULL);