I have two tables: BusinessViews: id createdAt businessId userId 1 2022-01-19 18:00:00 1 1 2 2022-01-19 18:02:00 1 1 3 2022-01-19 18:05:00 1 1 4 2022-01-19 18:05:50 1 1 5 2022-01-19 18:07:00 1 1 6 2022-01-19 18:08:00 1 1 7 2022-01-19 18:10:00 1 1 BusinessClients: id status createdAt userId createdBy businessI…
Tag: mysql
How to multiply two columns and add a new column to the database?
Which customer id bought maximum products? (hint : get sales by multiplying product_quantity and price_per_item fields) This is how the database looks like After adding the sales column I am not able to perform the sum operation and getting the following error: SELECT customerid, product_quantity * price_per_…
How to select data depending on data from the same table with custom field in SELECT?
I have a query to fetch closest airports to the airport: Where 55.966324 and 37.416573 are latitude and longitude of the airport I’m searching neighbour airports for. But in order to get those coordinates I would need to fetch that airport data first in a separate query which would slow things down. I w…
Using column from another table in trigger
I need to use a column from another table when i use trigger before insert but i get an error Unknown table in field list Here is an example code: the column from another table is “printing_time_hr” from table “slicer” Please help, what to do, how to use the column from another table A…
Trying to get sum of specific column cells for last 4 rows sum in MySQL
I am trying to get sum of capacity column of last 4 rows of a table in MySQL. My Table is : My SQL is : Result is giving different sum. It should be 150+200+250+300 = 900. I am looking for sum of the red circled numbers. Answer You could order using subquery and apply SUM in the outer query.
SQL – Ordering table with subquery for select
I am trying to do ordering on query with subquery for a name from uuid. Let’s have those two tables in MySQL: bans: id uuid time reason 1 c6b8eade-7db9-345b-b838-8f8361552cf5 1642369382 Test 2 0138c279-c5fa-3acd-adaa-8edb7b4569d8 1642384565 Spam 3 3c01262c-a3c3-3133-ba43-92a9ded01c27 1631876477 Hax user…
mysql error 1822 when trying to create new table with a FOREIGN KEY
trying the following code: Give back Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint ‘stock_ibfk_1’ in the referenced table ‘donation’. Why? how can I solve this problem? Answer to create a foreign key relationship, the parent table column on wh…
How to add or insert foreign key value in child table from parent table using insert statement
I want to use the primary key of parent table in my child tables. I have created a foreign key to connect QUESTION and ANSWER table with each other. below is code of my database: user table So, In USER table I have added a new value: Now in QUESTION table I want to add a new value and I
how to create new table in mysql with date default value for current date?
I am trying the following code: How can I make a default value for the date column? try the now(), but it is not valid.. Answer You can use current_date
GROUP BY within functions
I’m currently stuck with MySQL I would love to make this SQL to use the COUNT for each id_customer that was GROUP BYed as well. Picture of result And now it takes COUNT for every id_customer so it divides everything by 6 instead of 3 for id_customer 3, 2 for id_customer 1 and 1 for id_customer 66. Help …