I have two tables which you can also find in the SQL fiddle: The first table contains the Sales for each product. The second table contains the Categories. Now, I want to display all products and the average_sales_price_per_category next to each product. The result should look like this: I tried to go with the solution from this question but I
Tag: mysql
Change MySQL query execution order
I want to put a entry in the database with id of 2. In order to do that I’m moving all records having more than 2 to id + 1. Because id is a primary key. This query is failing saying : #1062 – Duplicate entry ‘3’ for key ‘PRIMARY’. I know this is happening because query execution starts from
How to merge 2 cells to generate a new value/text in an empty column cell?
I have a table with columns: firstName, lastName and emailAddress firstName and lastName are populated with values but emailAddress column is empty. How do I generate and insert an email address for …
Display average value of parent values next to each sub value
SQL Fiddle CREATE TABLE Campaigns ( Campaign_ID VARCHAR(255), Campaign_Category VARCHAR(255), Sales_Volume VARCHAR(255), Sales_Quantity VARCHAR(255) ); INSERT INTO Campaigns (…
Is it possible to translate this “join” and “as” query to Laravel PDO functions?
I am struggling to make this SQL query to a Laravel PDO query. Does anyone know how I can do this? (Especially the ‘AS’ statements and the naming in the joins). Query: Answer Directly use join() and select() method like this:
What is the functioning of “->” operator in MYSQL joins?
I’m using Sequelize, and trying to learn from the queries it constructs. I have some complicated one-to-many relationship between few models, so i come across this query: The query itself is not important, but i would like to understand the meaning of the “->” operator, like in: ScrumLists->ScrumCards.content AS ScrumLists.ScrumCards.content Answer In this context, the -> has no special meaning.
SQL Query with REGEXP to change URL strings dynamically
My DB table named “post” does look as follows So not every message row does contain an url and not every message with a [LINK]-tag does contain a proper url. Also there are enrties which have a longer ID, they should not be changed. Now i have to change every entry which has an ID length between 4 and 5
replace a column with another column when column is null in SELECT statement
Here is my dummy Schema in MYSQL: A column: which is a nullable string B column: which is a string but its not null I only want to select A columns but when facing a null, I want it to be replaced with the B column. How can I do that? this is my desired output Answer Use coalesce():
What’s best practice for normalisation of DB where a domain table has an “Other” option for free text?
I’m currently in the process of normalising a database for my company and one pattern I’m seeing a lot in this database is using a domain lookup table for a value but also allowing “Other” and storing the results in a separate column. My question is just whether there is perhaps a cleaner way of representing this? For context I’m
Select all orders except the max order for each distinct customer
Sorry for the poor formatting but as part of a larger problem, I have created a query that produces this table: How would I extract the rows that don’t have the highest id2 for each id1. What I want: I can only seem to figure out how to get rid of the max id2 overall but not for each distinct