Use Case statement inside queryBuilder-2
See original GitHub issueIssue 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:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I did this and it worked well:
It generated two queries:
And the result is correct.
I have the same problem. The query isn’t generated at all, just the message as @noteasybutclear provided.