Book Name Author Name No. of Copies Entry Date
Web Development Er. Gurbax Singh 7 2015-10-07
PHP Mr. Yash Arora 5 2015-10-06
DBMS Mr. Subhash Singla 2 2015-10-30
Data Structure Mr. Balwinder Singh 6 2015-11-14
Multimedia Dr. Latila Bhutani 3 2015-11-23
Graphics Designing Er. Gurpreet Kaur 6 2015-11-23
Note: I want sum of no. of copies
use SUM
SELECT SUM(no_of_copies) AS totalBooks FROM tablename
or Group by sum
SELECT SUM(no_of_copies) AS totalBooks FROM tablename group by BookName
Related
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.
Suppose I have this MySql table:
id
name
city
1
John
NYC
2
Albert
London
3
Joanna
Paris
4
Mike
LA
5
Norton
São Paulo
6
Pedro
Rio de Janeiro
7
August
Chicago
8
Carol
Miami
So I get this string and I would like to filter people with these ids:
"1,3,6,8"
Sometimes the string I got is different: "1,5,4" or "3,6,5,8,1" etc.
Any idea how to achieve this?
You would use a WHERE IN (...) construct in your query:
SELECT *
FROM yourTable
WHERE id IN (1, 3, 6, 8);
#Vanderlei based on your comment I will suggest not using a subquery, but a join because it performs much faster.
select products_table.id,products from products_table
inner join table_people
on products_table.id=table_people.id
WHERE table_people.city = 'London';
Demo: https://www.db-fiddle.com/f/7yUJcuMJPncBBnrExKbzYz/61
I have a problem in writing a MySQL query from my table INVOICES
I have 1 line per item purchased (ID, Date, Time, First, Last_Name, ITEM_PURCH, PRICE)
I would like to use this table to extract the customers who came into the shop.
For example, I have Zoe MARTIN who purchased 2 items the same day
Also, Cammille VELAZQUEZ who also purchase 2 items the same day
I would like to be able to SELECT with the question: PLEASE SORT THE CUSTOMERS LIST WHO CAME in the 30/01/2019 regardless the number of products purchased
With a result like this
46681 30/01/2019 09:09:24 Zoe MARTIN
46685 30/01/2019 09:39:08 Carmen SMITH
46686 30/01/2019 09:58:14 Valery SIMON
46689 30/01/2019 10:17:11 Camille VELAZQUEZ
46691 30/01/2019 10:24:30 Alexia SERR
46694 30/01/2019 10:49:40 Olivia ANABEL
46697 30/01/2019 11:13:18 Jennifer JOGHIN
Eliminating the MARTIN duplicate and the VELAZQUEZ
Original table with 2 duplicates
ID Date Time First Last_Name ITEM PURCH Price
46681 30/01/2019 09:09:24 Zoe MARTIN First purchase 175.00
46682 30/01/2019 09:09:57 Zoe MARTIN New proetct 175.00
46685 30/01/2019 09:39:08 Carmen SMITH Product 1 104.17
46686 30/01/2019 09:58:14 Valery SIMON Product 3 145.83
46689 30/01/2019 10:17:11 Camille VELAZQUEZ Product1 104.17
46690 30/01/2019 10:17:25 Camille VELAZQUEZ Product2 104.17
46691 30/01/2019 10:24:30 Alexia SERR Product 555 129.17
46694 30/01/2019 10:49:40 Olivia ANABEL Product 23 33.33
46697 30/01/2019 11:13:18 Jennifer JOGHIN Product 345 250.00
ANy help with building the table would be fantastic
I'm giving more information about this question.
I need to delete the duplicate lines of a customer came twice or more during the same day. But I want to keep the lines if the customer has come yesterday or one month ago (just removing the duplicates each day he came).
I would like to ask MySQL
"please make a list of customers who came at least once a day in the shop, from what you have in the invoice table" - I only need 1 line per customer per day (even if he purchased 2 or 3 items).
I want to be able to say:
From the beginning of this database until now, I got XXX different costumers who bought at least 1 thing. And here is the list of their names.
Try this
select a.ID, a.Date, a.Time, a.First, a.Last_Name from invoices a join ( select id,Date,concat(First, Last_Name) as fullname from INVOICES group by Date,fullname ) b on a.id=b.id;
Year team name lastname goals teamate goals
1875 Philadelphia Athletics George Hall 12 Bill Craver 11
1875 Philadelphia Athletics Bill Craver 11 George Hall 12
I'm getting output like this which is redundant.
I only want one player name and one teammate's name
If you just need one team name or player name, then use a "group by" clause on those columns, and pick the aggregated values of the other columns.
For more details, you may copy and paste your query here and that will help us understand the problem more precisely.
I am creating a library database and have four tables as follows;
I have been researching ways to work out the frequency in MySQL but after such as long time and misunderstanding I've decided to try get an example of how to work out the frequency on tables that I'll understand. Below are the four tables I am currently using.
I am looking to workout the loan frequency of every book that has been loaned 2 or more times. By doing this I am able to see how working out frequency would work when selecting specific values instead of all values.
From looking at my tables I would have to select the 'code' from the loan table, select all values that occur twice or more and then workout the frequency of the occurrence.
From my research I would decide to use an INNER JOIN to connect the tables, COUNT to count the number of values, GROUP BY to group the values and HAVING as WHERE may not be used. I am having trouble writing the query and continuously stumble upon errors. Could anyone use the example above to explain how they worked out the frequency of each book loaned two times or more? Thanks in advance
Table 1 - book
isbn title author
111-2-33-444444-5 Pro JavaFX Dave Smith
222-3-44-555555-6 Oracle Systems Kate Roberts
333-4-55-666666-7 Expert jQuery Mike Smith
Table 2 - copy
code isbn duration
1011 111-2-33-444444-5 21
1012 111-2-33-444444-5 14
1013 111-2-33-444444-5 7
2011 222-3-44-555555-6 21
3011 333-4-55-666666-7 7
3012 333-4-55-666666-7 14
Table 3 - student
no name school embargo
2001 Mike CMP No
2002 Andy CMP Yes
2003 Sarah ENG No
2004 Karen ENG Yes
2005 Lucy BUE No
Table 4 - loan
code no taken due return
1011 2002 2015.01.10 2015.01.31 2015.01.31
1011 2002 2015.02.05 2015.02.26 2015.02.23
1011 2003 2015.05.10 2015.05.31
1013 2003 2014.03.02 2014.03.16 2014.03.10
1013 2002 2014.08.02 2014.08.16 2014.08.16
2011 2004 2013.02.01 2013.02.22 2013.02.20
3011 2002 2015.07.03 2015.07.10
3011 2005 2014.10.10 2014.10.17 2014.10.20
You didn't specify the type of frequency, but this query calculates the number of loans per week for each book that was loaned more than once in 2014:
select b.isbn
, b.title
, count(*) / 52 -- loans/week
from loan l
join copy c
on c.code = l.code
join book b
on b.isbn = c.isbn
where '2014-01-01' <= taken and taken < '2015-01-01'
group by
b.isbn
, b.title
having count(*) > 1 -- loaned more than once