当前位置: 动力学知识库 > 问答 > 编程问答 >

mysql - Searching array element in Database

问题描述:

I want to create a table called user_ports which will contain all the ports the user has opened in the database.

Instead of calling each colum as "port_80", "port_100"

I was wondering if its possible to enter it to a colum which will be called opened_ports as an array.

But if I would like to get all the users with port 80 opened, how can I do that?

网友答案:

As far as I can understand, you just need to have 2 columns in the user_ports table: user_id which will reference the appropriate user, and opened_port, which will describe the specific opened port. Then you should probably declare the (user_id, opened_port) pair as a primary key. With such table structure, you could select the ids of users that opened 80 port by the following query:

SELECT user_id FROM user_ports WHERE opened_port = 80;

You COULD store all of the ports opened by user as CSV in a single record, but this will:

  1. require additional code that will deal with such trickery;
  2. complicate and slow down your database queries;
  3. denormalize your database (which should avoided).
分享给朋友:
您可能感兴趣的文章:
随机阅读: