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.

Use Case statement inside queryBuilder-2

See original GitHub issue

Issue type:

[x] question [ ] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [x] mysql / mariadb [ ] oracle [ ] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[ ] latest [ ] @next [x] 0.2.16 (or put your version here)

I’d like to use orderby in order of the values I want using the CASE WHEN statement.

I’ve already checked issue #3167 and we’ve gone ahead and there’s an error.

My code is as follows.

result = await getConnection()
          .getRepository(Post)
          .createQueryBuilder("post")
          .orderBy(_sortField, _sortOrder)
          .skip(_offset)
          .take(_limit)
          .leftJoinAndSelect("post.user", "user")
          .leftJoinAndSelect("post.products", "product")
          .leftJoinAndSelect("product.brand", "brand")
          .orderBy(`CASE WHEN product.category='SOMETHING' THEN 1 END`, "DESC") // test code, not work!
          .getMany();

Error codes are as follows:

Error: "CASE WHEN product" alias was not found. Maybe you forgot to join it?

Please help me.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

13reactions
hcesarcommented, Apr 5, 2019

I did this and it worked well:

const posts = await conn.manager
      .createQueryBuilder("post", "p")
      .addSelect("CASE WHEN p.id % 2 = 0 then -p.id else p.id end", "_rank")
      .leftJoinAndSelect("p.parent", "pr")
      .skip(2)
      .take(100)
      .orderBy('_rank')
      .getMany();

It generated two queries:

query: SELECT DISTINCT `distinctAlias`.`p_id` as "ids_p_id", `distinctAlias`._rank FROM (SELECT `p`.`id` AS `p_id`, `p`.`name` AS `p_name`, `p`.`description` AS `p_description`, `p`.`parentId` AS`p_parentId`, `pr`.`id` AS `pr_id`, `pr`.`name` AS `pr_name`, `pr`.`description` AS `pr_description`, `pr`.`parentId` AS `pr_parentId`, CASE WHEN `p`.`id` % 2 = 0 then -p.id else `p`.`id` end AS `_rank` FROM `post` `p` LEFT JOIN `post` `pr` ON `pr`.`id`=`p`.`parentId`) `distinctAlias` ORDER BY `distinctAlias`._rank ASC, `p_id` ASC LIMIT 100 OFFSET 2

query: SELECT `p`.`id` AS `p_id`, `p`.`name` AS `p_name`, `p`.`description` AS `p_description`, `p`.`parentId` AS `p_parentId`, `pr`.`id` AS `pr_id`, `pr`.`name` AS `pr_name`, `pr`.`description` AS `pr_description`, `pr`.`parentId` AS `pr_parentId`, CASE WHEN `p`.`id` % 2 = 0 then -p.id else `p`.`id` end AS `_rank` FROM `post` `p` LEFT JOIN `post` `pr` ON `pr`.`id`=`p`.`parentId` WHERE `p`.`id` IN (4, 2, 1, 3, 5, 7, 9) ORDER BY _rank ASC

And the result is correct.

2reactions
NikolaLukic1commented, Dec 14, 2021

I have the same problem. The query isn’t generated at all, just the message as @noteasybutclear provided.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use CASE statement inside queryBuilder · Issue #3167 - GitHub
Steps to reproduce or a small repository showing the problem: I was looking for a function to use CASE statement through the documentation...
Read more >
How to do a WHERE CASE WHEN in doctrine's queryBuilder
i have the following function using query builder:
Read more >
Select using Query Builder - typeorm - GitBook
When using the QueryBuilder , you need to provide unique parameters in your WHERE expressions. This will not work: const result = await...
Read more >
Using the SQL CASE Statement - Navicat
Basic Syntax. The CASE statement comes in two flavors: the first evaluates one or more conditions and returns the result for the first...
Read more >
Doctrine2 CASE WHEN with QueryBuilder - Google Groups
I am pretty new to Doctrine2. in Doctrine1.2 this was possible: Doctrine_Query::create()->addSelect('((CASE WHEN c.slug LIKE "'.$search.
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