是否可以从 mysql 列获取特定的信息?
假设我有一个 mysql 名称表,其中一列具有以下名称 -
| id | name |
| 1 | "Robert Downey Junior" |
我只想从该列部分名称中回显: "Downey"
我知道最好的做法这将是名字、名字和姓氏三个单独的列,但是如果全名在一列中,是否可以只提取该名称的一部分?
注意:我想指出,我需要在另一个地方提供全名,而将名称放入数据库的最简单方法就是全名 - 因为实际上有数千个名称。
Say I have a mysql table of names and one column has the following name inside it -
| id | name |
| 1 | "Robert Downey Junior" |
and I only want to echo from this column part of the name: "Downey"
I know the most optimal way of doing this would be to have three separate columns for first name, second name and surname, however is it possible to extract just a part of that name if the full name was inside one column?
Note: I would like to point out that I would need the full name in another place and the easiest way to put the names into the database is the full name - as there are literally thousands of names.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于所有名称的格式相同(第一个中间最后一个,空格分隔),您可以执行以下操作:
Since all the names are formatted the same (First Middle Last, space separated), you can do:
我认为你不能单独在 MySQL 中做到这一点。我会使用 PHP
I dont think you can do this in MySQL alone. I'd use PHP
当然,
只需对查询结果使用 php 爆炸函数即可。在空间上爆炸并使用结果数组的第二个元素。由于第一个数组元素为 0,因此为 1。
假设 db_connect() 函数正常工作:
$conn=db_connect();
$rs = mysql_query("从
表
中选择*,其中id=1;$data = mysql_fetch_assoc($rs);
$name_parts = 爆炸(" ", $data["name"]);
回声 $name_parts[1];
参考
:
http://php.net/manual/en/function.explode.php
Sure,
Just use the php explode function on the result of the query. Explode on space and use the 2nd element of the resulting array. Which is 1 since the first array element is 0.
Assuming a working db_connect() function:
$conn=db_connect();
$rs = mysql_query("select * from
table
where id=1;$data = mysql_fetch_assoc($rs);
$name_parts = explode(" ", $data["name"]);
echo $name_parts[1];
Reference:
http://php.net/manual/en/function.explode.php
这将正确解析具有多个中间名的人的中间名,例如:
将是
This will correctly parse middle names of people with multiple middle names, e.g:
will be