Using multiple MySQL joins to each get multiple values

Using multiple MySQL joins to each get multiple values

By : Pedro Roig
Date : November 17 2020, 11:52 AM
I hope this helps you . First, you can calculate the minimum and maximum local rates for each location in one subquery. Then, you can join back to the original data to get the client ids.
If there is only one client with the min/max, then the following should do what you want:
code :
select l.location, l.minlr, rmin.id, l.maxlr, rmax.id
from (select location, min(local_rate) as minlr, max(local_rate) as maxlr
      from rate r
      group by location
     ) l join
     rate rmin
     on rmin.location = l.location and rmin.local_rate = l.minlr join
     rate rmax
     on rmax.location = l.location and rmax.local_rate = l.maxlr;

Share : facebook icon twitter icon
Multiple Joins Mysql Doubles SUM values

Multiple Joins Mysql Doubles SUM values

By : user3132893
Date : March 29 2020, 07:55 AM
I wish did fix the issue. You've got duplicate rows, you can check this by removing the group by and the SUM(... from your query.
Change the query as follows and tell me if that fixes to problem.
code :
  cidades.id AS id,
  cidades.name AS municipio,
  SUM(conjuntos.n_uhs) AS uh,
  programas.id AS programa,
FROM conjuntos
INNER JOIN conjuntos_programas ON conjuntos_programas.conjunto_id = conjuntos.id
INNER JOIN programas ON programas.id = conjuntos_programas.programa_id
INNER JOIN cidades ON conjuntos.cidade_id = cidades.id
WHERE conjuntos.situation_id =  2
GROUP BY conjuntos.cidade_id
ORDER BY municipio ASC
Incorrect values with multiple left joins (MySQL)

Incorrect values with multiple left joins (MySQL)

By : AppleSoice
Date : March 29 2020, 07:55 AM
To fix this issue Your other reading is correct. You will need to put them into their own "subquery" for the join. The reason you are probably getting invalid values is that the materials table has multiple records per machine, thus causing a Cartesian result from your original based on hours. And you don't know which has many vs just one making it look incorrect.
So, I've written, and each inner-most query for pre-aggregating the woHours and woMaterial will produce a single record per "wo_id and machine_id" to join back to the wo table when finished. Each of these queries has the criteria on the single customer ID you are trying to run it for.
code :
WO    Machine ID    Machine   Hours   Material
1     1             CustX 1   2       0
2     4             CustY 4   2.5     6.5
3     4             CustY 4   1.2     .5
4     1             CustX 1   1.5     1.2
Machine   Hours   Material
CustX 1   3.5     1.2
CustY 4   3.7     7.0

      SUM( AllWO.Hours ) Hours,
      SUM( AllWO.Material ) Material
      ( SELECT
              CONCAT(customer.PREFIX, wo.machine_id) AS machine, 
              ROUND( COALESCE( PreSumHours.MachineHours, 0), 2) AS hours,
              ROUND( COALESCE( PreSumMaterial.materialHours, 0), 2) AS material
                 JOIN customer 
                    ON wo.customer_id = customer.id

                 LEFT JOIN ( select wohours.wo_id,
                                    SUM( wohours.length * wohours.price ) as machinehours
                                       wohours.wo_customer_id = 1
                                   AND wohours.date >= ( CURDATE() - INTERVAL DAY( CURDATE() - INTERVAL 1 DAY) DAY) - INTERVAL 11 MONTH
                                group by
                                   wohours.wo_machine_id ) as PreSumHours
                    ON wo.id = PreSumHours.wo_id 
                    AND wo.machine_id = PreSumHours.wo_machine_id 

                 LEFT JOIN ( select womaterial.wo_id,
                                    SUM( womaterial.length * womaterial.price ) as materialHours
                                       womaterial.wo_customer_id = 1
                                   AND womaterial.date >= ( CURDATE() - INTERVAL DAY( CURDATE() - INTERVAL 1 DAY) DAY) - INTERVAL 11 MONTH
                                group by
                                   womaterial.wo_machine_id ) as PreSumMaterial
                    ON wo.id = PreSumMaterial.wo_id 
                    AND wo.machine_id = PreSumMaterial.wo_machine_id 
              wo.customer_id = 1 ) AllWO
   group by
MySQL return multiple rows with multiple joins and multiple sums

MySQL return multiple rows with multiple joins and multiple sums

By : 林丽梅 lily
Date : March 29 2020, 07:55 AM
I hope this helps you . I am not entirely sure what you're going for, but here's a basic problem: you don't have a GROUP BY statement. When you use a grouping function like SUM, it tells mysql that you want to group results together. If you leave out GROUP BY, it groups everything into one row. If you want to have one row per user (I assume so, because you're selecting their names), you should say
code :
GROUP BY users.id
MySQL: Fetching non-null values using multiple joins

MySQL: Fetching non-null values using multiple joins

By : user1893200
Date : March 29 2020, 07:55 AM
it should still fix some issue You can do Inner Join between all the tables, using their relationships. Then, employ multiple level Order By clauses starting from the first table (all in Descending order, since you want topmost from all the tables). We use LIMIT 1 to get the first row after sorting, which will be topmost. Inner Join will ensure that any non-matching rows (null in the next table) will be ignored.
code :
SELECT t4.field2 
where t1.node = '5bbcdded'
multiple count and sum MySQL function returning wrong values with multiple joins in MySQL result

multiple count and sum MySQL function returning wrong values with multiple joins in MySQL result

By : greft
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , You are getting wrong output because, when you are joining based on studentid, you are getting multiple records from mpkids_paidstatus table for each student, which is adding up and returning a wrong output.
You can also write your query like following using subquery.
code :
SELECT A.id as student_id,
COUNT(DISTINCT B.id) as images, 
COUNT(DISTINCT C.id)+ COUNT(DISTINCT D.visitor_count)  as visits, 
count(DISTINCT E.id) + SUM(B.like_count) as likes, 
COUNT(DISTINCT A.Email)+COUNT(DISTINCT F.rel_email)  as emails, 
COUNT(DISTINCT A.Mobile)+COUNT(DISTINCT F.rel_mobile)  as moibles, 
(select SUM(Received) from mpkids_paidstatus ps where ps.student_id=a.id)  as Received 
FROM mpkids_students AS A 
LEFT JOIN mpkids_student_image_gallery AS B ON B.student_id = A.id 
LEFT JOIN mpkids_visitors AS C ON C.student_id = A.id 
LEFT JOIN mpkids_visitors_count AS D ON D.student_id = A.id 
LEFT JOIN mpkids_image_likes AS E ON E.student_id = A.id 
LEFT JOIN mpkids_relatives_data AS F ON F.student_id = A.id 

WHERE A.BranchId = 25 
Related Posts Related Posts :
  • MySQL join optimization - bypassing filesort
  • How to Select record with the maximum value in a GROUP BY?
  • MySQL Innodb Full text Proximity Search Gives Horrible Performance
  • MySQL best approach for db normalising, relationships and foreign keys
  • How to update one table based on the count value of another table while matching ids?
  • MAMP Pro 5.1.1-terminates MySQL when quitting
  • SQL - Get date of most votes cast
  • "Fuzzier" search in mysql
  • Tracing mysqld Using SystemTap
  • SELECT + INSERT + Query Cache = MySQL lock up
  • SQL vs. NoSQL for medium complexity search systems
  • SQL Error when creating table
  • Is it possible to write a query to compare rows to other rows in same table?
  • Display all duplicate records based multiple columns
  • summing time column in mysql
  • InnoDB: Cannot open table
  • Why is IN() considered O(logN) operation?
  • Can a deadlock occur on commit?
  • Is there a fast way to do a '%phrase%' search in any DB?
  • Is using an IN over a huge data set a good idea?
  • How can I know if a table is already arranged in ascending order in MySQL?
  • MySQL - Designing database for a simple game
  • Why am I getting "Host '' is not allowed to connect to this MySQL server"?
  • what type of password hash? possible mysql hash?
  • MySQL - Using a date range vs functions
  • Using IS NULL and COALESCE in OrderBy Doctrine Querybuilder
  • How to merge MySQL columns into one column if there are empty values in the table?
  • Cardinality Confusion
  • Renaming column multiple ids in SELECT
  • Group all from and to messages per user
  • How to handled the null values in WHERE IN condition in mysql
  • Is it okay to update primary key field to shift a record to the top?
  • Stop query through pdo
  • How to check whether particular value in field in MySQL exists
  • Execute a statement for every table in a database
  • insertion 74G data into mysql table cost more then 2 days, how to improve insert performance
  • Where can I see the differents "Hibernate Console Configurations" in Eclipse
  • MySql | relational database vs non relational database in terms of Performance
  • Exporting mysql database using mysqldump including procedures
  • Trigger MySQL not working
  • How do I update values for individual rows quickly?
  • MySQL join query with multiple where condition
  • MySQL stored procedure is not working
  • MySQL - Order By exact match at the end
  • Select max date or null date from same table MySQL
  • Best solution workaround for needing a mysql event on insert?
  • MySQL select several columns of several tables at the same time without using JOIN
  • Is it more efficient to have a table with one of its columns mostly empty or make a new table and refer to it?
  • mysql data truncated with ñ character
  • MySQL Distinct Active Users for the last month query
  • Get all rows that match another attribute in the same table
  • MySQL statement to get min value with max ID
  • DATEDIFF Current/Date for Last Record
  • What is the logic or procedure for combining multiple DB query's results into one elegant table?
  • Modify TIMESTAMP MySQL for use as Primary Key
  • Django - Efficiently bulk create inherited models
  • MySQL Dual table Join Dual table
  • Ruby on Rails - database where sorted by id
  • MySQL 5.6 : Access denied for user 'root' @ 'localhost' when password contains backslash
  • sql query case dateadd
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org