我是靠谱客的博主 等待保温杯,最近开发中收集的这篇文章主要介绍mysql 数组 或,MySQL中的数组,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

I want to store an array in a record.

Table1:

ID, Name, Friends (friends should be an array)

1, Bill, 2&3

2, Charles, 1&3

3, Clare, 1

I want to be able to do a search like this:

SELECT * FROM Table1 WHERE Friends='3'

to find everyone who has Clare listed as a friend

解决方案

Unless you have a really good reason for doing this, you should keep your data normalized and store the relationships in a different table. I think perhaps what you are looking for is this:

CREATE TABLE people (

id int not null auto_increment,

name varchar(250) not null,

primary key(id)

);

CREATE TABLE friendships (

id int not null auto_increment,

user_id int not null,

friend_id int not null,

primary key(id)

);

INSERT INTO people (name) VALUES ('Bill'),('Charles'),('Clare');

INSERT INTO friendships (user_id, friend_id) VALUES (1,3), (2,3);

SELECT *

FROM people p

INNER JOIN friendships f

ON f.user_id = p.id

WHERE f.friend_id = 3;

+----+---------+----+---------+-----------+

| id | name | id | user_id | friend_id |

+----+---------+----+---------+-----------+

| 1 | Bill | 1 | 1 | 3 |

| 2 | Charles | 2 | 2 | 3 |

+----+---------+----+---------+-----------+

2 rows in set (0.00 sec)

最后

以上就是等待保温杯为你收集整理的mysql 数组 或,MySQL中的数组的全部内容,希望文章能够帮你解决mysql 数组 或,MySQL中的数组所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(43)

评论列表共有 0 条评论

立即
投稿
返回
顶部