how to find the latest inserted data from two table using mysql - mysql

I have two table name
tbl_audio and tbl_video.
both tables have the same things. id,name,date, u_id.
Here u_id is a foreign key.
Now I want to show the last uploaded file from two table.
Suppose
this is tbl_audio
this is tbl_video
Here video3 is the last uploaded file according to date from two tables. I want to find this name(video3) by joining both tables.
thanks in advance.

You should union both tables and find the top 1 of they by date:
SELECT *
FROM
(
SELECT id, name, date, u_id FROM tbl_audio
UNION ALL
SELECT id, name, date, u_id FROM tbl_video
) as T
ORDER BY Date DESC
LIMIT 1
Also consider to use the one table to store all data say tbl_media with additional field media_type (audio or video).

Try use trigger. You can create log table to save last insert. Trigger AFTER INSERT will always update log table.
About create trigger you can read in documentation

You can use MAX() function and get the latest entry in the table like
select max(name) as latest_video
from tbl_video;
If you want data from both table on the matched u_id then perform a JOIN between them and use the max() function to get the latest audio/video file name.

Related

SQL: How to update values of table one and implement the change in table 2 as welll?

I have two tables, and i am trying to update the first table based on the value from second table. Table 1 contains the Sum of total weight and table two contains weight. when added up these values in table 2, it then shows total in table 1
For example if I update the foreign key in table 2 then the Amount in table should update. Please refer the screenshot attached.
My first table sql:
select LodingZoneID, Finaltotal from TransitList
My Second table sql:
SELECT `Suburb`, `LodingZoneID`, Total FROM `GenerateRun`
There is no reason for table 1. You can just run the query:
select fk, sum(val)
from table2
group by fk;
If you really need to store the sum in table 1, then you would be using triggers on table2 to handle insert/update/delete. This is rather cumbersome and it is generally better to do the calculation when needed, rather than trying to store the results in advance.
Or, You can create a View with desired output
-CREATE VIEW 'TransitList' AS
SELECT LodingZoneID, SUM(Total) FinalTotal FROM GenerateRun
GROUP BY LodingZoneID;
Whenever you want to access data, you can use
-SELECT LodingZoneID, FinalTotal FROM TransitList;

Get all values from table and reuse them to create rows in other table

I have a table (People) with columns (id, name, year). Is there any way I can get all the ids ( SELECT id FROM people ) and use them for creating new rows in other table (people_id, additional_info). I mean that for every id in table People I want to add new row in other table containing the id and some additional information. Maybe something like for-loop is needed here ?
Well, usually you have information on a person and write one record with this information. It makes litte sense to write empty records for all persons. Moreover, when it's just one record per person, why not simply add an information column to the people table?
Anyway, you can create mock records thus:
insert into people_info (people_id, additional_info)
select id, null from people;
Insert into targetTable (id, additional_info)
select id,'additionalinfo' from PEOPLE
No for loop is needed, Just use the above query.
You can use INSERT ... SELECT syntax for MySQL to insert the rows into people_info table (documentation here).
e.g.
insert into people_info(...)
select (...) from people; <and posibly other tables>

MySQL Keep record with most current date

I created a table w_provider_remove to keep the Provider_no and max(prov_effective_Date) of the records in w_providers_load because I have duplicate providers but some have expired. I later try to join the w_providers_load table and the w_provider_Remove table and insert the records into the w_providers_main table. The problem is I get back to many records because it turns out I have more than one record for the particular provider with the same effective date. How can I limit it so it only inserts one of them? Or maybe there is another way to go about this were I do not need 3 tables to accomplish this task
Truncate w_provider_remove;
insert into w_provider_remove
select provider_no as provider_no, max(PROV_DATE_EFFECTIVE) as prov_date_effective
from w_provider_load
group by provider_no;
Truncate w_provider_main;
INSERT INTO w_provider_main
Select l.*
from w_provider_load as l
inner JOIN w_provider_remove as r on l.provider_no = r.provider_no AND l.prov_date_effective = r.prov_date_effective;
If you want to limit the rows in the table to one effective date per provider, then create a unique index:
create unique index w_provider_load_provider_effectivedate on w_provider_load(provider_no, prov_effective_date);
This will generate an error if two records for the same provider have the same effective date in the load.
You can do the same thing for the main table:
create unique index w_provider_main_provider_effectivedate on w_provider_main(provider_no, prov_effective_date);

MySQL: Delete records whose id is not in the result set of another query

This should be a pretty simple thing, but I'm quite new to (My)SQL.
Basically, given a customers table with attributes id, client, etc., where the client field is not necessarily unique, I want to eliminate rows where the client field is a duplicate of a previous value.
The following:
SELECT MIN(id) FROM customers GROUP BY client
returns the unique id's of the rows I want. I want everything else out.
I tried
DELETE FROM customers WHERE customer.id NOT IN
(SELECT MAX(id) FROM customers GROUP BY client)
to no avail. (ERROR 1093 (HY000): You can't specify target table 'customers' for update in FROM clause).
Why doesn't it work and what do I need to do to accomplish my goal?
Thank you.
You could create a temporary table that holds the values you want to remove. Then your delete query could be based on that temporary table.

SQL: flavours of INSERT INTO

(MySQL) I am trying to migrate a "subscription" table into 3 new tables: "product", "subscription", "actual" where "actual" is the name of the actual product, say, newsletter. The subscription has a FK reference to product and the actual has FK reference into subscription.
I know the INSERT INTO SELECT statement can copy data probably from many table to one; is there a statement to do the opposite, one to many tables for my case?
I'm not aware of an SQL statement that will do what you want. Just do several INSERT INTO SELECTs one after the other. It may be faster to do them one at a time anyway.
I think you can use three seperate insert into select statements. First you convert the product table, then the subscription where you can use an embedded select to find the id in the product table:
insert into subscription (some_column, FK_id,...)
select something, (select id from product where <your where clause>),...
and finally convert the actual table using an embedded select to get the id from the subscription table.