Search from keys in foreign table - mysql

Dharmik soni

I am using nestjs and typeorm (Mysql). I have two tables users and categories, and in category table i have a foreign key linked to userid. What i want to do is search firstName from user table while querying the category table.

I have 2 search fields category name and user name. For searching category name what i did is

const query = this.createQueryBuilder('category');
if (categoryName) {
      query.andWhere('category.categoryName LIKE :categoryName', {
        categoryName: `%${categoryName}%`,
      });
    }

And for searching username

if (userName) {
      query.andWhere('category.user.firstName LIKE :userName', {
        userName: `%${userName}%`,
      });
    }

But the above one is giving me error. Any idea how to do it using typeorm and nestjs ? Thanks for the help !

Eranga Heshan

You need to join category with user first. Try something like this:

const query = this.createQueryBuilder('category');

if (categoryName) {
  query.andWhere('category.categoryName LIKE :categoryName', { categoryName: `%${categoryName}%` });
}

if (userName) {
  query
    .leftJoinAndSelect('category.user', 'user')
    .andWhere('user.firstName LIKE :userName', { userName: `%${userName}%` });
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

MYSQL: select by two foreign keys from the same table

MySQL database with two foreign keys in one table

(MySQL) 2 foreign keys referencing the same table

Mysql: using two foreign keys to the same table

MySQL Create Table with Multiple Foreign Keys

Delete data from table with foreign keys as primary

Delete from multiple table with foreign keys

MySQL data from foreign table

Select all foreign keys from the foreign key table in django

MySQL - Search table that contains Foreign key id with foreign key value

query to search between two foreign keys in a self join table

Multiple columns from the same table in another table as foreign keys

Join table of foreign keys

Table Structure and foreign keys

Adding to a table with foreign keys

SQL DDL: Recursive table with two foreign keys (MySQL)

mysql queries on two foreign keys referencing the same table

How do I programmatically create two foreign keys in mysql table

MySQL - custom, additional columns based on foreign keys with other table

How to create a mysql table with multiple foreign keys and multiple columns

MySQL table with "calculated values" and keys that are primary and foreign at the same time

Working in mysql with foreign keys

Duplicate foreign keys in mysql

MySQL two foreign key from same table

Entity Framework Code First - two Foreign Keys from same table

SQL: Selecting data from another table using foreign keys

2 Foreign Keys Into a New Table from Different Entities Hibernate

join on two foreign keys from same table in SQL

Get all foreign keys from ORM query in parent table - Django