Compare Table with a monitoring point - mysql - mysql

i have a table that contains product data with prices. The table updated every 5 - 10 minutes.
Now i have a cronjob that makes a monitoring point by copying the table.
So i get a copy of the data every morning.
Now i want to check my prices at the evening by comparing them with the monitor table.
How could my query look like to get the two prices.
The query should show products where the prices are different.
Table a
[id] - [product_no] [product_desc] [product_price] - [product_stock]
Table b (from cronjob]
[id] - [product_no] [product_desc] [product_price] - [product_stock]

You ask for a basic join between two tables. Here is how a join can be performed:
SELECT TableA.product_no
, TableA.product_price AS TableAPrice
, TableB.product_price AS TableBPrice
FROM TableA
INNER JOIN TableB
ON TableA.id = TableB.id
WHERE TableA.product_price != TableB.product_price
Try to learn how joins are working in general, this is a very important thing to know when working with databases.

Related

Trigger minusing from one table to another table MYSQL

So, i have two table at the minute...
Freezing - (id, et1, straw, eps) &
Inventory - (id, et1, owner, total)
I need it so that when someone enters something into Freezing, and whatever is entered into 'EPS' in freezing will take off 'total' in Inventory. The common column between these 2 tables is the et1 number. Currently the statement I'm using below enters, however it does not affect the total in Inventory. If someone could help me see where i'm going wrong that would be brilliant.
UPDATE et1209_inventory
SET total = total - "et1202_freezing.EPS"
WHERE "et1202_freezing.EPS" = total
If I understand you correctly it would probably be the easiest to just create a third table that joins Freezing and Inventory:
create table if not exists Output_table as
select i.id, i.et1, i.owner, f.eps - i.total
from Freezing f
join Inventory i on f.id = i.id;

Compare differences in 2 tables

I am running a MySQL Server on Ubuntu, patched up to date...
In MySQL, I have 2 tables in a database. I am trying to get a stock query change working and it kind of is, but it's not :(
What I have is a table (table A) that holds the last time I have checked stock levels, and another table (table B) that holds current stock levels. Each table has identical column names and types.
What I want to do is report on the changes from table B. The reason is that there are about 1/2 million items in this table - and I cannot just update each item using the table as a source as I am limited to 100 changes at a time. So, ideally, I want to get the changes - store them in a temporary table, and use that table to update our system with just those changes...
The following below brings back the changes but shows both Table A and Table B.
I have tried using a Left Join to only report back on Table B but I'm not a mysql (or any SQL) guy, and googling all this... Can anyone help please. TIA. Stuart
SELECT StockItemName,StockLevel
FROM (
SELECT StockItemName,StockLevel FROM stock
UNION ALL
SELECT StockItemName,StockLevel FROM stock_copy
) tbl
GROUP BY StockItemName,StockLevel
HAVING count(*) = 1
ORDER BY StockItemName;
The query below spit out records that have different stock level in both table.
SELECT s.StockItemName, s.StockLevel, sc.StockLevel
FROM stock s
LEFT JOIN stock_copy sc ON sc.Id = s.Id AND sc.StockLevel <> s.StockLevel
ORDER BY s.StockItemName
ok - I solved it - as there wasn't a unique ID on each table that could be matched, and rather than make one, I used 3 colums to create the unique ID and left joined on that.
SELECT sc.StockItem, sc.StockItemName, sc.Warehouse, sc.stocklevel
FROM stock s
LEFT JOIN stock_copy sc ON (sc.StockItem = s.StockItem AND sc.StockItemName = s.StockItemName AND sc.Warehouse = s.Warehouse AND sc.StockLevel <> s.StockLevel)
having sc.StockLevel is not Null;

fast way to get number of records in mysql

I'm writing a query in mysql to join two tables. And both tables have more than 50,000 records.
Table EMP Columns
empid,
project,
code,
Status
Table EMPINFO
empid,
project,
code,
projecttype,
timespent,
skills
In each table there is candidate key [empid, project, code]
So when I join the table using INNER join
like this INNER JOIN
ON a.empid = b.empid
and a.project = b.project
and a.code = b.code
I'm getting the result, but if I add count(*) in outer query to count number of records, it takes lot of time something connection gets failed.
Is there any way to speed up to get number of records ?
And I would like to hear more suggestions to speed up inner join query as well having same candidate key in both tables.
INDEX(empid, project, code) -- in any order.
Are these tables 1:1? If so, why do the JOIN in order to do the COUNT?
Please provide SHOW CREATE TABLE. (If there are datatype differences, this could be a big problem.)
Please provide the actual SELECT.
How much RAM do you have? Please provide SHOW VARIABLES LIKE '%buffer%';.

How can i execute this query faster?

This is my query:
create table vi_all as
select
d.primaryid, d.age, d.gndr_cod, d.wt, d.wt_cod, d.reporter_country,
dr.primaryiddrug, dr.role_cod, dr.drug_name,
r.primaryidreac, r.pt,
o.primaryidoutc, o.outc_cod,
i.primaryidindi, i.indi_pt
FROM demo d,
drug dr,
reac r,
outc o,
indi i;
Each table contains at least 80K records and more than 20 fields so its getting really tough to execute select statement on multiple tables; and i just want 4 or 3 fields from each table so i thought of this, but the above query has taken more than 5 hours but still has not given back any result.
My crystal ball says you need something like this:
create table vi_all as
select
d.primaryid, d.age, d.gndr_cod, d.wt, d.wt_cod, d.reporter_country,
dr.primaryiddrug, dr.role_cod, dr.drug_name,
r.primaryidreac, r.pt,
o.primaryidoutc, o.outc_cod,
i.primaryidindi, i.indi_pt
FROM demo d
LEFT JOIN drug dr ON d.drug=dr.id
LEFT JOIN reac r ON d.reac=r.id
LEFT JOIN outc o ON d.outc=o.id
LEFT JOIN indi i ON d.indi=i.id;
As far as I can tell your query is selecting all results from all tables but not assocating them in anyways so, you can maybe get duplicate data in the newly created table. Also, if you have some good foreign keys to associate those tables, the performance will be considerably better.

MySQL Join with many (fields) to one (secondary table) relationship

I have a query I need to perform on a table that is roughly 1M records. I am trying to reduce the churn, but unfortunately there is a UNION involved (after i figure this join out), so that may be a question for another day.
The records and data I need to get reference 3 fields in a table that need each pull a description from another table and return it in the same record, but when i do the Inner join i was thinking, it either returns only 1 field fromt he other table, or multiple records from he original table.
Here are some screen shots of the tables and their relationship:
Primary table containing records (1 each) with the physician record I want to pull, including up to 3 codes that can be listed in the "taxonomy" table.
Secondary table containing records (1 each) with the "Practice" field I want to pull.
A Quick glance of the relationship i'm talking about
I presume that if perform an inner join matching the 3 fields in the physicians table, that it will have to iterate that table multiple times to pull each taxonomy code .. but I still can't even figure the syntax to easily pull all of these codes instead of just 1 of them.
i've tried this:
SELECT
taxonomy_codes.specialization,
physicians.provider_last_name,
physicians.provider_first_name,
physicians.provider_dba_name,
physicians.legal_biz_name,
physicians.biz_practice_city
FROM
taxonomy_codes
INNER JOIN physicians ON physicians.provider_taxonomy_code_1 = taxonomy_codes.taxonomy_codes OR physicians.provider_taxonomy_code_2 = taxonomy_codes.taxonomy_codes OR physicians.provider_taxonomy_code_3 = taxonomy_codes.taxonomy_codes
First, the query churns a lot and it only returns one taxonomy specialty result which I presume is because of the OR in the join statement. Any help would be greatly appreciated.
Thank you,
Silver Tiger
You have to join the taxonomy_codes table multiple times:
SELECT p.provider_last_name, p...., t1.specialization as specialization1, t2.specialization as specialization2, t3.specialization as specialization3
FROM physicians p
LEFT JOIN taxonomy_codes t1 ON t1.taxonomy_codes = provider_taxonomy_code_1
LEFT JOIN taxonomy_codes t2 ON t2.taxonomy_codes = provider_taxonomy_code_2
LEFT JOIN taxonomy_codes t3 ON t3.taxonomy_codes = provider_taxonomy_code_3