question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wrong SQL in Join Statement

See original GitHub issue

See following joins:

		[Test, NorthwindDataContext]
		public void Test(string context)
		{
			using (var db = new NorthwindDB(context))
			{
				DataConnection.TurnTraceSwitchOn(TraceLevel.Verbose);
				DataConnection.WriteTraceLine += (a, b) => { System.Diagnostics.Debug.WriteLine(a + " -> " + b); };
				var jj = from o in db.Order
					join c in db.Customer on o.CustomerID equals c.CustomerID into cg
					from c in cg.DefaultIfEmpty().Take(1)
					select new {o, c};

				var res = jj.ToList();

				var jj2 = from o in db.Order
					join c in db.Customer.Take(1) on o.CustomerID equals c.CustomerID into cg
					from c in cg.DefaultIfEmpty()
					select new { o, c };

				var res2 = jj2.ToList();
			}
		}

Both Querys create the same SQL, wich I think is not correct. Don’t know if the first one is possible in SQL.

Generated SQL:

	DataConnection: -- NorthwindSqlite SQLite
	SELECT
		[o].[OrderID],
		[o].[CustomerID],
		[o].[EmployeeID],
		[o].[OrderDate],
		[o].[RequiredDate],
		[o].[ShippedDate],
		[o].[ShipVia],
		[o].[Freight],
		[o].[ShipName],
		[o].[ShipAddress],
		[o].[ShipCity],
		[o].[ShipRegion],
		[o].[ShipPostalCode],
		[o].[ShipCountry],
		[t1].[CustomerID1] as [CustomerID11],
		[t1].[CompanyName] as [CompanyName1],
		[t1].[ContactName] as [ContactName1],
		[t1].[ContactTitle] as [ContactTitle1],
		[t1].[Address] as [Address1],
		[t1].[City] as [City1],
		[t1].[Region] as [Region1],
		[t1].[PostalCode] as [PostalCode1],
		[t1].[Country] as [Country1],
		[t1].[Phone] as [Phone1],
		[t1].[Fax] as [Fax1]
	FROM
		[Orders] [o]
			LEFT JOIN (
				SELECT
					[cg].[CustomerID] as [CustomerID1],
					[cg].[CompanyName],
					[cg].[ContactName],
					[cg].[ContactTitle],
					[cg].[Address],
					[cg].[City],
					[cg].[Region],
					[cg].[PostalCode],
					[cg].[Country],
					[cg].[Phone],
					[cg].[Fax]
				FROM
					[Customers] [cg]
				LIMIT 1
			) [t1] ON [o].[CustomerID] IS NULL AND [t1].[CustomerID1] IS NULL OR [o].[CustomerID] = [t1].[CustomerID1]

	DataConnection: Execution time: 00:00:00.0135097

	DataConnection: -- NorthwindSqlite SQLite
	SELECT
		[o].[OrderID],
		[o].[CustomerID],
		[o].[EmployeeID],
		[o].[OrderDate],
		[o].[RequiredDate],
		[o].[ShippedDate],
		[o].[ShipVia],
		[o].[Freight],
		[o].[ShipName],
		[o].[ShipAddress],
		[o].[ShipCity],
		[o].[ShipRegion],
		[o].[ShipPostalCode],
		[o].[ShipCountry],
		[cg].[CustomerID1] as [CustomerID11],
		[cg].[CompanyName] as [CompanyName1],
		[cg].[ContactName] as [ContactName1],
		[cg].[ContactTitle] as [ContactTitle1],
		[cg].[Address] as [Address1],
		[cg].[City] as [City1],
		[cg].[Region] as [Region1],
		[cg].[PostalCode] as [PostalCode1],
		[cg].[Country] as [Country1],
		[cg].[Phone] as [Phone1],
		[cg].[Fax] as [Fax1]
	FROM
		[Orders] [o]
			LEFT JOIN (
				SELECT
					[t1].[CustomerID] as [CustomerID1],
					[t1].[CompanyName],
					[t1].[ContactName],
					[t1].[ContactTitle],
					[t1].[Address],
					[t1].[City],
					[t1].[Region],
					[t1].[PostalCode],
					[t1].[Country],
					[t1].[Phone],
					[t1].[Fax]
				FROM
					[Customers] [t1]
				LIMIT 1
			) [cg] ON [o].[CustomerID] IS NULL AND [cg].[CustomerID1] IS NULL OR [o].[CustomerID] = [cg].[CustomerID1]

	DataConnection: Execution time: 00:00:00.0005012

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:16 (16 by maintainers)

github_iconTop GitHub Comments

github_iconTop Results From Across the Web

What is wrong with this inner join sql query
JOINs can, when multiple rows on the one table match a single row in the other table, cause the result set to have...
Read more >
Are You Making These SQL Join Mistakes? | by Marie Truong
Mistake #1: Joining on Something That Is Not Unique​​ Let's say you have a table called cities that includes a list of cities...
Read more >
6 hidden SQL mistakes to avoid
3. Wrong joins ... Joins are a key SQL functionality that helps us combine data from two tables. But when we use the...
Read more >
MySQL JOIN query produces wrong result
Join the two tables and fetch values and show the complaint and complaint's reply as a single result set. But the condition is...
Read more >
SQL Join : A Common Mistake - Ani - Medium
RIGHT JOIN : This join returns all the rows of the table on the right side of the join and matching rows for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found