Ask Your Question
3

How can I retrieve the value of an array in MySQL by specifying its key?

asked 2021-04-28 11:00:00 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-07-25 17:00:00 +0000

woof gravatar image

MySQL does not support arrays directly, but you can use a combination of tables and SQL queries to achieve similar functionality.

One approach is to create a separate table to store your array values, using the key as a column:

CREATE TABLE my_array (
  key_name VARCHAR(255) PRIMARY KEY,
  value VARCHAR(255)
);

Then, you can insert values into this table:

INSERT INTO my_array (key_name, value)
VALUES ('key1', 'value1'), ('key2', 'value2'), ('key3', 'value3');

To retrieve the value of a specific key, you can use a SELECT query:

SELECT value FROM my_array WHERE key_name = 'key2';

This will return the value associated with the "key2" key.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-04-28 11:00:00 +0000

Seen: 17 times

Last updated: Jul 25 '21