I have three tables
1.PURCHASE TABLE
id
Country
Medicine
Quantity
Purchase date
0
Canada
Aspirin
9
26/01/2022 14:16:59
1
Canada
VitaminD
10
19/07/2021 14:16:59
2
Usa
Calcium
4
19/06/2021 14:16:59
3
Canada
VitaminC
8
06/08/2022 14:16:59
4
Argentina
Calcium
10
05/12/2021 14:16:59
2.PRICES TABLE
id
Country
Year
Medicine
Price
0
USA
2020
Aspirin
14
1
Canada
2020
Aspirin
18
2
Mexico
2020
Aspirin
10
3
Brasil
2020
Aspirin
11
4
Argentina
2021
Aspirin
18
PRICE_PURCHASE TABLE (PIVOT TABLE)
price_id
purchase_id
How can I create and update the fields in both using laravel query builder or mysql ?
Thank you very much for your time
You can use attach method to insert into the pivot table. This method accept an array, if you want a multiple insert.
Also, you can use detach to remove a record from the pivot table.
I think, if you want to insert or update the main tables you have to do it directly, without pivot table.
Here are the links from docs:
Attaching - Detaching
This method is using Eloquent ORM. If you want to use you should use subqueries or JOIN to 'connect' pivot table with the main tables.
There are 2 tables that are supposed to have unique product codes but I came across a case where the product code ended up the same for two of the records in another table. I would like to get the product information based on the latest product year. Let's assume I have the following tables.
Table 1:
recall
id
product_year
product_code
yes
200
2019
3222333
no
201
2020
3222333
yes
202
2021
4332233
no
203
2021
5446553
yes
204
2018
6556677
Table 2:
recall
id
product_year
product_code
no
100
2019
2245643
yes
101
2020
1234543
no
102
2017
4332233
yes
103
2022
5446553
yes
104
2018
3344566
Table 3 contains only unique product code information and other updated information based on the latest product year. For eg: product code 3222333 has only one entry even though Table 1 has 2 entries for 3222333. But the problem comes for codes 4332233 and 5446553 which are present in both Table 1 and Table 2.
Table 3:
country
id
product_code
Brazil
301
3222333
Indonesia
302
4332233
Argentina
303
6556677
Chile
304
2245643
Brazil
305
1234543
Chile
306
5446553
USA
307
3344566
It is known that Table 3 doesn't contain correct data. But I would like to generate a query in mysql to get all the product codes from Table 3 and get the product code related information based on the latest product year. Earlier I was using something like COALESCE(table1.recall, table2.recall, null) assuming that only one value will be present and it works. But for the codes 4332233 and 5446553, it will always pick table 1 column value as recall values for the same product codes are present in both tables 1 and 2. How should I deal with this problem so that I get the data only based on the latest product year?
Expected output:
country
product_code
recall
Brazil
3222333
no
Indonesia
4332233
yes
Argentina
6556677
yes
Chile
2245643
no
Brazil
1234543
yes
Chile
5446553
yes
USA
3344566
yes
It did take me some time to realise how convenient it is to use UNION in this case. Below is the code written and tested in workbench.Please note this is currently the best answer I can come up with based on the limited table structure and sample data you provided. Since you say that it's possible for the same product_code to have the same product_year in both table1 and table2 and you have other means to determine the latest one, please tweak the code as you see fit. Hope this helps solve your issue.
select country, recall,ta.product_code as product_code
from
(select product_code,product_year,recall
from table1 t1
union
select product_code,product_year,recall
from table2 t2
) ta
join
(select product_code p_c,max(product_year) mx_y
from
(select product_code,product_year
from table1 t1
union
select product_code,product_year
from table2 t2
) t_union
group by product_code
) tb
on ta.product_code=p_c and ta.product_year=mx_y
join table3 t3
on p_c=t3.product_code
;
I created a report with multiple fields and I have a field named Ageddays that calculated the aging days on each row of data. I need to group by Ageddays <31, <61, <91, and <121. The report should look like this below and any helps is appreciated.
Header ClientName Item Purdate Ageddays Location Salesperson Paid
Group by**Ageddays < 31**
Detail XYX LLC toy1 12/21/2017 10 ABC address Sam No
AAA LLC toy2 11/11/2017 20 ABC address Sam No
**Ageddays < 61**
CDF LLC toy3 08/21/2017 40 EEB address Rey No
AAA co. toy4 07/11/2017 50 YYY address Sam No
**Ageddays < 91**
MMY Co. toy3 06/21/2017 60 EEB address Eri No
GGG Co. toy4 05/11/2017 70 YYY address Abe No
Create an additional column based on the condition you want. I mean the new column value should be
Case when agedays<31 then 1
When agedays<61 then 2
When agedays<91 then 3
When agedays<121 then 4
Else 4 end as groupcategory.
Then you can group the data using this groupcategory field.
how to represent the below Excel table as a Mysql DB structure format where in a column has multiple sub columns as given below
Name Mon-Fri Sat Sun
Asia UK USA Asia UK USA Asia UK USA
Name1 0.25 0.25 0.5 0.5 - - - 0.5 -
Name2 0.5 0.5 1 1 - - - - -
Name3 0.25 0.25 0.5 0.5 - - - 0.5 -
You usually do this by adding a Time column to the data and "normalize" it:
Name Region Time Amount
------|-------|--------|-------
Name1 Asia Mon-Fri 0.25
Name1 Asia Sat 0.5
Name1 Asia Sun -
Name2 UK Mon-Fri 0.25
Name2 UK Sat -
...
See https://en.wikipedia.org/wiki/Database_normalization as well
For this first create a table of Days
tbl_days
id,
dayname
Then Create a group with these days. Group may be with one day or multiple days
tbl_group
id,
groupname,
days (make it comma separted with day id)
Then create a country master
tbl_countries
id,
country_name
Then assign the calculation like below
tbl_calculation
id,
group_id,
country_id,
value (nullable)
You can go with following Table Structure
tbl_Region -
ID , Name
1,UK
2,Asia
tbl_Date_allocation -
ID,WEEK_DAY_NAME
1, Mon
2 , Wed
3 ,...
tbl_types
Id, Name
1, Name 1
2, Name 2
......
tbl_allocation
id,region_id,date_allocation_id,type_id,value
1,1,2,1,0.25
id - auto increment
region_id,date_allocation_id,type_id - foreign keys to other table
What you have are two entities: something-that-has-a-name, let's call them People, and Geographic Area -- three of which are the values Asia, UK and USA. Then you have represented relationships between People and GAs. As seen from your data, a Person may relate to 0 or more GAs and a GA may relate to 0 or more people. Such a many to many relationship is maintained by an intersection or cross table (two names for the same thing). You further show two aspects of this relationship: a term ("Mon-Fri", "Sat" and "Sun") and an Amount.
The fact that Term values are days of the week are not really significant. There are three possible values so let's call them 1, 2 and 3. Amount looks like it could be any floating point value.
create table PersonGO(
PersonID int not null references People( ID ),
GAID int not null references Areas( ID ),
Term int not null check( Term in (1, 2, 3 )),
Amount float not null,
constraint PK_PersonGO primary key( PersonID, GRID )
);
The data stored in such table would look like this:
PersonID GAID Term Amount
1 1 1 0.25 -- Name1 Asia Mon-Fri
1 1 2 0.50 -- Name1 Asia Sat
1 2 1 0.25 -- Name1 UK Mon-Fri
1 2 3 0.50 -- Name1 UK Sun
1 3 1 0.50 -- Name1 USA Mon-Fri
2 1 1 0.50 -- Name2 Asia Mon-Fri
2 1 2 1.00 -- Name2 Asia Sat
2 2 1 0.50 -- Name2 UK Mon-Fri
2 3 1 1.00 -- Name2 USA Mon-Fri
3 1 1 0.25 -- Name3 Asia Mon-Fri
3 1 2 0.50 -- Name3 Asia Sat
3 2 1 0.25 -- Name3 UK Mon-Fri
3 2 3 0.50 -- Name3 UK Sun
3 3 1 0.50 -- Name3 USA Mon-Fri
Where there is no relationship for a particular person to a particular area on a particular term, such as Name1 with UK on Saturday, there is no entry. You would not have an entry with 0 or NULL in the Amount field. That would be unnecessary.
As your sample data looks like one week's worth of data and there would probably be one set of these entries for each week of a year, the table might have another field, WeekNum, with possible values such as 201601 for the first week of 2016, 201602 for the second week and so forth. You have a good deal of flexibility here. You could just as well merge this with the Term field: 2016011 for Mon-Fri of the first week, 2016012 for Sunday of the first week and so on. You could come up with something entirely different. Whatever makes the most sense to you.
How can I duplicate a bunch of records in mySQL?
I have records like this:
ID - name - year
-----------------
1 jasmine - 1999
2 peter - 1999
3 fleur - 1999
How can I duplicate all the names with WHERE year='1999' to a new row with a new year?
ID - name - year
-----------------
1 jasmine - 1999
2 peter - 1999
3 fleur - 1999
4 jasmine - 2000
5 peter - 2000
6 fleur - 2000
Any ideas?
INSERT INTO mytable (name, year)
SELECT (name, 2000) FROM mytable WHERE year = 1999;
Try with this pls. In place of 2000 you can use as you wish.
INSERT INTO duplicate_entry (name, year)
SELECT name, '2000' FROM duplicate_entry where year='1999'
Best of luck.