Not displaying dates but instead moving them to a different column - mysql

Having an issue with a join code where the code is executing but is giving me a warning that it is truncating my PROD_INTO_DATE but I noticed that it was entered into the PROD_CAT_CD column.
INSERT INTO `goac`.`product`
(`PROD_ID`,
`PROD_NM`,
`PROD_SKU_NO`,
`PROD_CAT_CD`,
`PROD_PACKAGE_SIZE_NO`,
`PROD_INTRO_DT`)
select distinct
c.PROD_ID, c.PROD_NM, c.PROD_SKU_NO,c.PROD_INTRO_DT,
s.PROD_PACKAGE_SIZE_NO, s.PROD_CAT_CD
FROM ods_product AS c
join ods_sale_large as s
on s.PROD_NM = c.PROD_NM;
select * from product
Where the data is being inserted
The PROD_CAT_CD has data that should be there.
And where the join is coming from it should be fine because it sees the date as date.
from the ods_product

You need to insert the columns in the same order as in the insert column list:
INSERT INTO `goac`.`product` (`PROD_ID`, `PROD_NM`, `PROD_SKU_NO`, `PROD_CAT_CD`, `PROD_PACKAGE_SIZE_NO`, `PROD_INTRO_DT`)
select distinct c.PROD_ID, c.PROD_NM, c.PROD_SKU_NO, s.PROD_CAT_CD, s.PROD_PACKAGE_SIZE_NO, c.PROD_INTRO_DT
from ods_product c join
ods_sale_large s
on s.PROD_NM = c.PROD_NM;
Once I had a colleague that I respected very much. She initially had the same confusion with insert . . . although you list the columns and the select also has column names, the matching is by position rather than by name.

Related

Need to write two query one for insert the record and other for update the record in main table

I need to write two separate query one for insert the record in main table and other one for update the record in same main table.
Condition : I need to insert and update the main table from other table which is exactly same in structure as main table.Below are rules for doing the same
1: We need to match three condition from both the table like provider_id,NPI AND TAX .If we found match in both the table then it will update the same record in main table from other table.
2:If any of above three check doesnt match then it will insert the full record in main table from other table.
Note : We need not to use the not exists condition for doing the same.It will be good if we use outer join for doing the same.
Please find my code which is inserting the duplicate record each time which it should not..
INSERT INTO inc.prov_stext_ma_archive (SEQ_ID,SOURCE_ID,SOURCE_TYPE,SUBMITTER_PLAN_ID,PROVIDER_ID,ID_TYPE,STATE_LICENSE_NUMBER,MEDICAID_NUMBER,FULL_NAME,LAST_NAME,FIRST_NAME,OFFICE_ADDRESS_STREET,OFFICE_ADDRESS_CITY,OFFICE_ADDRESS_STATE,OFFICE_ADDRESS_ZIP,MAILING_ADDRESS_STREET,MAILING_ADDRESS_CITY,MAILING_ADDRESS_STATE,MAILING_ADDRESS_ZIP,PROVIDER_TYPE,FILLER,EFFECTIVE_DATE,TERM_DATE,NON_PAR_INDICATOR,NETWORK_ID,IPA_PMG_ID,PANEL_OPEN_INDICATOR,PROVIDER_DEA_NUMBER,PROVIDER_TYPE_DESCRIPTION,NATIONAL_PROVIDER_IDENTIFIER,MEDICARE_ID,SOCIAL_SECURITY_NUMBER,NAPB_NUMBER,TAX_ID,IPA_PMG_ID_TYPE,GENDER_CODE,PRIMARY_CARE_ELIG_IND,STATE_SPECIALTY,STATE_SPECIALTY_DESCRIPTION,MCE_PROV_TYPE,MCE_PROV_TYPE_DESCRIPTION,VEND_SPECIALTY,COS_UR_IND,COS_CREDENTIALS,COS_PANEL_NBR,COS_PROV_IDENT,TAXONOMY_CODE,CONTRACT_TYPE,STATUS,ERROR_ID,ERROR_DESCRIPTION,USER_DEF_1,USER_DEF_2,USER_DEF_3,USER_DEF_4,USER_DEF_5,
INSERT_DATETIME,INSERT_PROCESS,INSERT_USER,UPDATE_DATETIME,UPDATE_PROCESS,UPDATE_USER,TYPE_SPEC_STATUS)
SELECT W.SEQ_ID, W.SOURCE_ID, W.SOURCE_TYPE, W.SUBMITTER_PLAN_ID, W.PROVIDER_ID, W.ID_TYPE, W.STATE_LICENSE_NUMBER, W.MEDICAID_NUMBER, W.FULL_NAME, W.LAST_NAME, W.FIRST_NAME, W.OFFICE_ADDRESS_STREET, W.OFFICE_ADDRESS_CITY, W.OFFICE_ADDRESS_STATE, W.OFFICE_ADDRESS_ZIP, W.MAILING_ADDRESS_STREET, W.MAILING_ADDRESS_CITY, W.MAILING_ADDRESS_STATE, W.MAILING_ADDRESS_ZIP, W.PROVIDER_TYPE, W.FILLER, W.EFFECTIVE_DATE, W.TERM_DATE, W.NON_PAR_INDICATOR, W.NETWORK_ID, W.IPA_PMG_ID, W.PANEL_OPEN_INDICATOR, W.PROVIDER_DEA_NUMBER, W.PROVIDER_TYPE_DESCRIPTION, W.NATIONAL_PROVIDER_IDENTIFIER, W.MEDICARE_ID, W.SOCIAL_SECURITY_NUMBER, W.NAPB_NUMBER, W.TAX_ID, W.IPA_PMG_ID_TYPE, W.GENDER_CODE, W.PRIMARY_CARE_ELIG_IND, W.STATE_SPECIALTY, W.STATE_SPECIALTY_DESCRIPTION, W.MCE_PROV_TYPE, W.MCE_PROV_TYPE_DESCRIPTION, W.VEND_SPECIALTY, W.COS_UR_IND, W.COS_CREDENTIALS, W.COS_PANEL_NBR, W.COS_PROV_IDENT, W.TAXONOMY_CODE, W.CONTRACT_TYPE, W.STATUS, W.ERROR_ID, W.ERROR_DESCRIPTION, W.USER_DEF_1, W.USER_DEF_2, W.USER_DEF_3, W.USER_DEF_4, W.USER_DEF_5,
A.INSERT_DATETIME, W.INSERT_PROCESS, W.INSERT_USER, W.UPDATE_DATETIME, W.UPDATE_PROCESS, W.UPDATE_USER, W.TYPE_SPEC_STATUS
FROM INC.MA_PROV_ST_EX_FULL W LEFT OUTER JOIN inc.prov_stext_ma_archive A ON( A.provider_id = W.provider_id
AND A.NATIONAL_PROVIDER_IDENTIFIER = W.NATIONAL_PROVIDER_IDENTIFIER
AND A.TAXONOMY_CODE = W.TAXONOMY_CODE AND A.PROVIDER_ID IS NULL)
WHERE W.SOURCE_ID = 'COSMOS'

Selecting Data from a different Table through the value of a other table

REPLACE INTO `Lehrling` (`idLehrling`,`Nachname`,`Vorname`,`Aufnahmedatum`,`Austrittsdatum`,`Klasse`,`Klassensprecher`,`Betriebe_idBetriebe`,`Ausbildungsberufe_idAusbildungsberufe`,`Credentials_idCredentials`) VALUES (1,'Krahn','Daniel','09.02.2015','31.12.2015','FI31',0,1,3,1);
REPLACE INTO `Credentials` (`idCredentials`,`Benutzername`,`Passwort`,`Anlegedatum`) VALUES (1,'krahnd','osz123','10.02.2015');
My goal is to select Nachname,Vorname from 'Lehrling' where Klasse is 'FI31' and also select Benutzername,Passwort from Credentials through the 'idCredentials' at the same time.
This process is called joining:
Select Lehrling.Nachname, Lehrling.Vorname, Credentials.Benutzername, Credentials.Passwort
From Lehrling
inner join Credentials on Credentials.idCredentials = Lehrling.Credentials_idCredentials
Where Lehrling.Klasse = 'FI31'

MySQL inner join, check from different table

I have 2 tables formatted as below:
INSERT INTO `mixture` (`id`, `item`) VALUES
(1,'water'),
(2,'gas'),
(3,'oil'),
(4,'ice');
another table
INSERT INTO `check` (`name`, `seen`) VALUES
('Nadia','[2][3]'),
('Omer','[1][4][2]');
result needed:
How do I get the result to show this?
Nadia will only see information that has mixture.id 1 & 4, while
Omer will only see information that has mixture.id 3
Each time they see the result, mixture.id will be added to their check.seen status, so that they will not see the same information in the future.
This is what I have done so far:
SELECT
mixture.*,
check.seen,
check.name
FROM mixture
INNER JOIN check
WHERE check.seen not like '%[mixture.id]%'
Thanks in advance
Please make my day.
The index should be a numeric type like a integer not a string, no quotes around the numbers
Your another table will have a row for each mixture known by the person
('Nadia',2)
('Nadia',3)
('Omer',1)
('Omer',4)
('Omer',2)
A select for Nadia will return a record set with 2 records, one for each mixture she knows, 3 for Omer
Select seen FROM anothertable where name = 'Nadia'
a join will return the correct mixture item string from your first table.
http://www.w3schools.com/sql/sql_join.asp

how to perform a table join when the other table has many rows corresponding to one row in mother table

I have two table --> tbl_book_details and tbl_table_traking
tbl_book_details has columns bd_book_code,
bd_isbn,
bd_title,
bd_edition,
bd_author,
bd_publisher,
bd_supplier,
bd_page,
bd_price_type,
bd_cost_price,
bd_price,
bd_Tax,
bd_covering,
bd_availability,
bd_keywords,
bd_notes,
bd_details,
bd_news_latter,
bd_etDate,
bd_weight,
bd_expire_date,
bd_status
tbl_table_traking has columns
tt_id,
tt_action,
tt_table,
tt_record_id,
tt_on_date,
tt_user,
tt_status
the process is a trigger is defined on tbl_book_details which in case of insert/modify insert the data in tbl_table_traking for traking when and who has modified the records.
till now i have been using following query which is not a join -->
SELECT
tbl_books_details.bd_book_code AS bkid,
tbl_books_details.bd_isbn,
tbl_books_details.bd_title AS title,
-- This part is what I believe is slowing down my query
(SELECT
tt_on_date
FROM
tbl_table_tracking
WHERE tt_action = 'MODIFY'
AND tt_record_id = tbl_books_details.bd_book_code ORDER BY tt_on_date) AS bd_etdate
it was working fine when the records count were below 3 million, but now script time out is occurring.
I have made the index on tbl_table_traking on 'tt_ondate' and on tt_action,
If there any way i can convert it to a join or improve the performance?
the table traking query is returning the mostrecent date on which the record was modified.
my database is in mysql.
You should be able to do something like this.
SELECT
tbl_books_details.bd_book_code AS bkid,
tbl_books_details.bd_isbn,
tbl_books_details.bd_title AS title,
tbl_table_traking.tt_on_date
FROM
tbl_books_details
INNER JOIN tbl_table_tracking
tbl_books_details.bd_book_code = tbl_table_tracking.tt_record_id
WHERE tt_action = 'MODIFY';
Hope this helps...

Trouble Creating a view in MySQL

I'm trying to create a view to display the info I have in tables. The trouble I'm having is joining two tables and then displaying the data separately.
I have two tables: one is tbl_videos and the other is tbl_categories_videos. tbl_videos has two category fields, both of which are taken from tbl_categories_videos. the category_ids display properly in tbl_videos but when I create a view, I can't get the category name to display properly.
The closest I can get to it working is when both category_1 and category_2 display the same value in the view, but the should be different.
I've been staring at the screen for far too long now so i'm probably missing something simple.
Anyway, here's the SQL i have for the view:
CREATE VIEW `VIDEOS_view` AS
SELECT `tbl_videos`.`videos_id` AS `videos_id`,
`tbl_videos`.`date` AS `date`,
`tbl_videos`.`author` AS `author`,
`tbl_videos`.`photo_credit` AS `photo_credit`,
`tbl_categories_videos`.`category_videos_name` AS `category_1`,
`tbl_categories_videos`.`category_videos_name` AS `category_2`,
`tbl_videos`.`thumb` AS `thumb`,
`tbl_videos`.`image_1` AS `image_1`,
`tbl_videos`.`video_embed` AS `video_embed`,
`tbl_videos`.`title` AS `title`,
`tbl_videos`.`sub_title` AS `sub_title`,
`tbl_videos`.`section_1` AS `section_1`,
`tbl_videos`.`section_2` AS `section_2`,
`tbl_videos`.`embed` AS `embed`
FROM ((`tbl_videos` join `tbl_categories_videos` on (
(`tbl_videos`.`category_id_1` AND
`tbl_videos`.`category_id_2` =`tbl_categories_videos`.`category_videos_id`
))))
any help would be much appreciated.
Try something like this:
CREATE VIEW `VIDEOS_view` AS
SELECT `tbl_videos`.`videos_id` AS `videos_id`,
`tbl_videos`.`date` AS `date`,
`tbl_videos`.`author` AS `author`,
`tbl_videos`.`photo_credit` AS `photo_credit`,
(select category_videos_name from tbl_categories_videos where category_videos_id = tbl_videos.category_id_1) AS `category_1`,
(select category_videos_name from tbl_categories_videos where category_videos_id = tbl_videos.category_id_2) AS `category_2`,
`tbl_videos`.`thumb` AS `thumb`,
`tbl_videos`.`image_1` AS `image_1`,
`tbl_videos`.`video_embed` AS `video_embed`,
`tbl_videos`.`title` AS `title`,
`tbl_videos`.`sub_title` AS `sub_title`,
`tbl_videos`.`section_1` AS `section_1`,
`tbl_videos`.`section_2` AS `section_2`,
`tbl_videos`.`embed` AS `embed`
FROM `tbl_videos`
With this query you just select the name of the category from the second table through the existing foreign keys tbl_videos.category_id_1 and tbl_videos.category_id_2 with nested select statements. So for each id it looks up tbl_category_videos for the matching category_videos_name.
The problem with your query was that you can't join a table on two different columns because then mysql don't know on which id it has to join resulting in no join if the ids are different.