是否可以从 mysql 列获取特定的信息?

发布于 2024-12-16 00:12:10 字数 299 浏览 2 评论 0原文

假设我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

同展鸳鸯锦 2024-12-23 00:12:10

由于所有名称的格式相同(第一个中间最后一个,空格分隔),您可以执行以下操作:

$result = mysql_query( 'SELECT name FROM table WHERE id = 1');
$row = mysql_fetch_assoc( $result);
$name = explode( ' ', $row['name']);
echo $name[1]; // "Downey"

Since all the names are formatted the same (First Middle Last, space separated), you can do:

$result = mysql_query( 'SELECT name FROM table WHERE id = 1');
$row = mysql_fetch_assoc( $result);
$name = explode( ' ', $row['name']);
echo $name[1]; // "Downey"
剪不断理还乱 2024-12-23 00:12:10

我认为你不能单独在 MySQL 中做到这一点。我会使用 PHP

$name = explode(" ", $result['name']);
echo "Hi, " . $name[1];

I dont think you can do this in MySQL alone. I'd use PHP

$name = explode(" ", $result['name']);
echo "Hi, " . $name[1];
っ左 2024-12-23 00:12:10

当然,

只需对查询结果使用 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

浅唱ヾ落雨殇 2024-12-23 00:12:10
$res = mysql_query("select name from myDB");

while ($row = mysql_fetch_assoc($res)) {
   $middlename = preg_match("#^[ ]+(?:Mrs|Mr)\.?[^ ]+ (.+) [^ ]+[ ]+$#i", row[0]);
   print_r($middlename[0]);
}

这将正确解析具有多个中间名的人的中间名,例如:

Kelvin de la Cruz Gam-Jensen

将是

de la Cruz
$res = mysql_query("select name from myDB");

while ($row = mysql_fetch_assoc($res)) {
   $middlename = preg_match("#^[ ]+(?:Mrs|Mr)\.?[^ ]+ (.+) [^ ]+[ ]+$#i", row[0]);
   print_r($middlename[0]);
}

This will correctly parse middle names of people with multiple middle names, e.g:

Kelvin de la Cruz Gam-Jensen

will be

de la Cruz
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文