How to specify the parent query field from within a subquery in MySQL?

How about: $query = “SELECT p1.id, (SELECT COUNT(1) FROM post_table p2 WHERE p2.parent_id = p1.id) as num_children FROM post_table p1 WHERE p1.parent_id = 0”; or if you put an alias on the p1.id, you might say: $query = “SELECT p1.id as p1_id, (SELECT COUNT(1) FROM post_table p2 WHERE p2.parent_id = p1.id) as num_children FROM post_table … Read more

SQL Oracle LEFT JOIN and SUBQUERY error: ORA-00905: missing keyword

In Oracle we don’t include the AS when declaring a table alias. Instead of ) AS TABLE_RESOLVERS write ) TABLE_RESOLVERS This is one example when Oracle syntax is more restrictive than some other flavours of SQL. It is also inconsistent with the declaration of column aliases, which is unfortunate but almost certainly it’s too complex … Read more

How to use an Alias in a Calculation for Another Field

That method doesn’t work in SQL Server. You can accomplish the same thing in a couple different ways: 1.) Use the code for each aliased column instead of the alias: (SELECT COUNT(*) FROM UserEvent UE WHERE UE.EventTypeID = 1 AND UE.PracticeID = au.PracticeID AND (UE.EventDate BETWEEN @Date1 and @Date2) – COUNT(CASE WHEN udi.DevicePlatform = ‘iOS’ … Read more

What is the difference between a LATERAL JOIN and a subquery in PostgreSQL?

What is a LATERAL join? The feature was introduced with PostgreSQL 9.3. The manual: Subqueries appearing in FROM can be preceded by the key word LATERAL. This allows them to reference columns provided by preceding FROM items. (Without LATERAL, each subquery is evaluated independently and so cannot cross-reference any other FROM item.) Table functions appearing … Read more