Chuyển tới nội dung
Trang chủ » Using Mysql Json Array Field To Join On A Table In Knex Update

Using Mysql Json Array Field To Join On A Table In Knex Update

You are looking for information on the topic “Using MySQL JSON array field to join on a table in knex”. boviptaynguyen.com will gather information to answer your questions in this article. Please read this article to the end to find the information you want to see. You can also check out some other related articles here: 226 new posts updated Knex js, Knex select all, Knex npm, Knex update multiple rows, Knex cheat sheet, Knex insert, Knex join multiple tables, Query in knex.

View information about the Using MySQL JSON array field to join on a table in knex

Information about this question

  • Question: Using MySQL JSON array field to join on a table in knex
  • Time: Asked 51 minute ago
  • Modified: Updated 147 hours ago
  • View: 2304

Using MySQL JSON array field to join on a table in knex

You can use the JSON_EXTRACT function in MySQL to extract a specific value from a JSON array stored in a column. Here’s an example using Knex to join a table based on a value in a JSON array:

javascript
knex.select('orders.*', 'customers.name') .from('orders') .join('customers', function() { this.on('orders.customer_ids', 'like', knex.raw("CONCAT('%', JSON_EXTRACT(customers.ids, '$[0]'), '%')")) })

In this example, we have a table called orders that contains a JSON array field called customer_ids, and we want to join it with a table called customers that has a column called ids that also contains JSON arrays.

The JSON_EXTRACT function is used to extract the first value ($[0]) from the customers.ids array, and the CONCAT function is used to add wildcards to the beginning and end of the resulting string, so that we can use the LIKE operator to match it with the orders.customer_ids field.

This should generate the following SQL statement:

sql
SELECT orders.*, customers.name FROM orders INNER JOIN customers ON orders.customer_ids LIKE CONCAT('%', JSON_EXTRACT(customers.ids, '$[0]'), '%')

Note that using JSON arrays in this way can make queries less efficient than using a traditional relational database schema, so it’s important to consider the trade-offs before using this approach.

Watch videos related to “Using MySQL JSON array field to join on a table in knex”

Knex JS Cheat-sheet Migration, Query, Join and all about Knex with Node JS

Images related to Using MySQL JSON array field to join on a table in knex

Found 18 Using MySQL JSON array field to join on a table in knex related images.

Javascript - Create A Nested Return Model With Knex.Js - Stack Overflow
Javascript – Create A Nested Return Model With Knex.Js – Stack Overflow
Sql - Mysql/Json: Compare An Array With The List Of A Column In Json Format  And Count The Number Of Terms That Match - Stack Overflow
Sql – Mysql/Json: Compare An Array With The List Of A Column In Json Format And Count The Number Of Terms That Match – Stack Overflow
Mysql Insert With Json Doesn'T Work / Er_Wrong_Value_Count_On_Row · Issue  #1800 · Knex/Knex · Github
Mysql Insert With Json Doesn’T Work / Er_Wrong_Value_Count_On_Row · Issue #1800 · Knex/Knex · Github
Json Data Type Support In Mysql V5.7.X And Sqlite 3.9 · Issue #1036 · Knex/ Knex · Github
Json Data Type Support In Mysql V5.7.X And Sqlite 3.9 · Issue #1036 · Knex/ Knex · Github

You can see some more information related to Using MySQL JSON array field to join on a table in knex here

Comments

There are a total of 212 comments on this question.

  • 394 comments are great
  • 476 great comments
  • 333 normal comments
  • 130 bad comments
  • 60 very bad comments

So you have finished reading the article on the topic Using MySQL JSON array field to join on a table in knex. If you found this article useful, please share it with others. Thank you very much.

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *