search

Home  >  Q&A  >  body text

How to add a column of type array using phpMyAdmin or sql

How to use phpMyAdmin to add a column of type array Know Change the table name and add column listid integer array; Change table name and add column listid integer []; Not working

P粉012875927P粉012875927285 days ago521

reply all(1)I'll reply

  • P粉764003519

    P粉7640035192024-03-27 20:40:13

    As mentioned in the comments, arrays are not types. You can choose to use a separate table to hold the elements in the array and have them have a foreign key that references the original table, or parse the array into a string each time and store it as text, depending on your needs.

    CREATE TABLE orders (
      id INT NOT NULL PRIMARY KEY,
      description TEXT,
      reference TEXT
      -- This is where you'd want to add your list of order lines
    );
    
    -- Instead, we'll create an orderline table referring back to the orders
    CREATE TABLE orderlines (
      id INT NOT NULL PRIMARY KEY,
      description TEXT,
      order_id INT REFERENCES orders(id)
    );
    

    Now you can put the array values ​​(I'm assuming for now the order lines) into their own separate table. To query them you can do this

    SELECT * FROM orders
    LEFT JOIN orderlines ON orderlines.order_id = orders.id;
    

    You can use subqueries to be smart enough to return an array, especially in your application.

    reply
    0
  • Cancelreply