Skip to content

Tag: mysql

Retrieve columns from three tables

I want to use columns from three different tables and use them to calculate how much each customer has ever spent. tblCustomer(CustomerID) tblOrder(CustomerID, ProductID, Amount) tblProduct(ProductID, Price) So I want to filter out the orders made by a customer, check what product they ordered and what amount…

Convert nested select to join

Account Table: Answer Your query is probably more efficient than a join/group by version, but it is equivalent to: I should note that if all banks have loans (which seems reasonable) no JOIN is necessary:

Sql to fetch records only if related other table records exist

Table: user id compId 1 comp1 2 comp1 Table: Company id name comp1 coke comp2 pepsi need a MYSQL query which should fetch company record only if it has one or more users, when passed a company id. I will have other where conditions on company table. Can this be achieved by joins? example 1: query(comp1) resul…

MySQL merge two tables with the same IDs

In table_A I have data from this week, the IDs started from 1 up to 5000. In table_B I have data from last month, whereas the first let’s say 3000 IDs conflict with the ones in table_A, and it’s not the same data for all of the IDs (ID 1 in table_A is not the same as ID 1 in

SQL: How to divide column by counts?

So I have this query This will print something like device_id paper_count 1 5000 2 10000 And I have this other other query that will return the number of times the toner has been changed according to some date. I used count to count the number of times it’s been changed. This will print something like d…

Find rows where sum of columns from multiple rows is greater than X

The best way to explain this question is through the real life example it would solve. I have two tables, Match Overview and Match Attributes Match overview looks like While match attributes looks like Where match_id is a foreign key for id in the match overview table, and attribute id is the integer represen…