MySQL join query with multiple where condition

MySQL join query with multiple where condition

By : Karanja
Date : November 22 2020, 10:54 AM
With these it helps I have 3 tables. I need to join those 3 and get 2 fields from each table. And there will be few where conditions. Where condition is for date range. Even if one table has the the result I need to show it along with other table data showing as 0. I have tried using inner join. But what it does is taking only 1st where condition and if no result in first it will not go for next conditions. My table structures and required output are shown below. , Try this:
code :
SELECT S.site_id, 
       IFNULL(t1.table1_Amount, 0) AS table1_Amount, 
       IFNULL(t2.table2_Amount, 0) AS table2_Amount, 
       IFNULL(t3.table3_Amount, 0) AS table3_Amount, 
      (IFNULL(t1.table1_Amount, 0) + IFNULL(t2.table2_Amount, 0) + IFNULL(t3.table3_Amount, 0)) AS total 
LEFT OUTER JOIN ( SELECT t1.site_id, SUM(t1.amount) AS table1_Amount 
                  FROM table1 t1
                  WHERE t1.date >= '01/12/2014' AND t1.date <= '31/12/2014'
                  GROUP BY t1.site_id
                ) AS t1 ON S.site_id = t1.site_id
LEFT OUTER JOIN ( SELECT t2.site_id, SUM(t2.amount) AS table2_Amount 
                  FROM table2 t2
                  WHERE t2.date >= '01/12/2014' AND t2.date <= '31/12/2014'
                  GROUP BY t2.site_id
                ) AS t2 ON S.site_id = t2.site_id
LEFT OUTER JOIN ( SELECT t3.site_id, SUM(t3.amount) AS table3_Amount 
                  FROM table1 t3
                  WHERE t3.date >= '01/12/2014' AND t3.date <= '31/12/2014'
                  GROUP BY t3.site_id
                ) AS t3 ON S.site_id = t3.site_id;

Share : facebook icon twitter icon
Mysql query like query dosent send accurate result in join with where condition from others table

Mysql query like query dosent send accurate result in join with where condition from others table

By : NonameDude
Date : March 29 2020, 07:55 AM
hop of those help? I have two tables users and user groups. , Try this:
code :
SELECT `u`.`id`, `u`.`fname`, `u`.`lname`, `u`.`customer_id`, `u`.`email`, `u`.`partner_id`, `u`.`campaign_promocode`, `u`.`utm_medium`, `u`.`referrral_source`, `u`.`is_active`, `u`.`created_at`, `ug`.`group_id`
FROM (`users` as u)
JOIN `users_groups` as ug ON `ug`.`user_id` = `u`.`id`
WHERE (`ug`.`group_id` =  '99' OR `ug`.`group_id` =  '100' )
AND `u`.`is_active` IN ('1', '2') 
AND  `u`.`fname`  LIKE '%sandeep%'
ORDER BY `u`.`id` desc
LINQ to SQL multiple left join query with subquery at join condition

LINQ to SQL multiple left join query with subquery at join condition

By : Colby Johnson
Date : March 29 2020, 07:55 AM
I hope this helps . That is because you are selecting Authorization_ID and Payment_ID which are of type int into jla and jlp respectively . Try the below query and let me know how it worked for you.
code :
var leaseList = (from l in leases.tblfLeaseDetails
                             join a in leases.tblfAuthorizations on l.Lease_Detail_ID equals a.Lease_Detail_ID into la
                             from jla in
                                 (from aj in leases.tblfAuthorizations
                                  where aj.Lease_Detail_ID == l.Lease_Detail_ID
                                  orderby aj.Authorized_Date descending
                                  select aj).Take(1).DefaultIfEmpty()
                             join p in leases.tblfPayments on l.Lease_Detail_ID equals p.Lease_Detail_ID into lp
                             from jlp in 
                                 (from pj in leases.tblfPayments
                                  where pj.Lease_Detail_ID == l.Lease_Detail_ID
                                  orderby pj.Payment_Date descending
                                  select pj).Take(1).DefaultIfEmpty()
                             join v in leases.tblvVendors on l.Vendor_ID equals v.Vendor_ID into lv
                             from jlv in lv.DefaultIfEmpty()
                             join c in leases.tblvCounties on l.County_ID equals c.County_ID into lc
                             from jlc in lc.DefaultIfEmpty()
                             select new LeaseViewModel()
                                 Lease_Detail_ID = l.Lease_Detail_ID,                                     
                                 Vendor_Name = jlv.Vendor_Name,                                  
                                 County = jlc.County,
                                 Authorization = jla.Authorized,
                                 Payment_Date = jlp.Payment_Date
mysql query inner join condition where id IN

mysql query inner join condition where id IN

By : Repays
Date : March 29 2020, 07:55 AM
Any of those help Not entirely sure what you mean, but I'm guessing you can use some variant of UPDATE JOIN, basic syntax is like:
code :
UPDATE table1 t1
  JOIN table2 t2
    ON t2.table1_id = t1.id
   SET t1.status = 1,
       t2.status = CASE 
         WHEN t1.columnX = 1 THEN 1 /* Update if columnX is 1 */
         ELSE t2.status             /* Use old status if not  */
       END CASE
 WHERE t1.id IN (...)
UPDATE table1 t1
   SET t1.status = 1
 WHERE t1.id IN (...)   

UPDATE table2 t2
  JOIN table1 t1
    ON t1.id = t2.table1_id
   AND t1.columnX = 1
   SET t2.status = 1
 WHERE t2.table1_id IN (...) 
MySQL join query with IF CONDITION

MySQL join query with IF CONDITION

By : nilaynitish10
Date : March 29 2020, 07:55 AM
it helps some times Use COALESCE:
code :
select cs.cust_id, coalesce(cs.cust_name, co.comp_name) contact
from customer cs
inner join company co
on cs.comp_id = co.comp_id;
select cs.cust_id, coalesce(nullif(cs.cust_name,''), co.comp_name) contact
from t_customer cs
inner join t_company co
on cs.comp_id = co.comp_id;
mysql select multiple avg columns using where in condition and join query

mysql select multiple avg columns using where in condition and join query

By : Ammar
Date : March 29 2020, 07:55 AM
hope this fix your issue This is similar to the solutions in need to return two sets of data with two different where clauses but uses AVG instead of SUM. The main difference is that you don't use ELSE 0 in the CASE, because you don't want to average 0 for all the rows with a different raingItemId. AVG() will just ignore those null rows.
code :
    AVG(CASE WHEN ratingItemId='1' THEN rs.score END) AS Academics,
    AVG(CASE WHEN ratingItemId='2' THEN rs.score END) AS Area_of_study,
    AVG(CASE WHEN ratingItemId='3' THEN rs.score END) AS Campus_Facilities,
    AVG(CASE WHEN ratingItemId='4' THEN rs.score END) AS Acommadatoin,
    AVG(CASE WHEN ratingItemId='5' THEN rs.score END) AS Sports_Facilities,
    i.instituteName, i.location, c.countryName, i.siteAddress, i.overallRatings
FROM institutions AS i
INNER JOIN countries AS c ON i.countryId=c.countryId
LEFT JOIN reviews AS r ON r.instituteId = i.instituteId
LEFT JOIN rating_score AS rs ON rs.reviewsId = r.reviewsId
WHERE i.instituteId IN (1, 2, 4, 15)
GROUP BY i.instituteId
Related Posts Related Posts :
  • 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 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
  • Click on one button multiple times and mySql
  • Select daily amount of specific values from same column
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org