gpt4 book ai didi

php mysql 数据库项名称从另一个数据库调用

转载 作者:行者123 更新时间:2023-11-30 01:35:17 25 4
gpt4 key购买 nike

我有一个关于如何使用 id 和项目名称与数据库中的 2 个表进行关联的问题

我有 2 个数据库,其中一个是:

CREATE TABLE `alchimie` (
`id` smallint(5) unsigned NOT NULL auto_increment,
`id_potiune` tinyint(3) unsigned NOT NULL default '0',
`nume_potiune` varchar(40) NOT NULL default '',
`nivel_potiune` tinyint(3) unsigned NOT NULL default '1',
`nivel_caracter` tinyint(2) unsigned NOT NULL default '1',
`obiecte_necesare` tinytext NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

这是插入数据的表格

Image 1 http://img705.imageshack.us/img705/4522/55040113.png

第二个数据库的一部分是

 CREATE TABLE `iteme` (
`obiect` int(11) NOT NULL auto_increment,
`nume` text collate utf8_polish_ci NOT NULL,
------------------------------------------------
PRIMARY KEY (`obiect`),
KEY `tip` (`tip`,`pret_cumparare`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8

(obiect in my case is id )

我不知道如何从 php id 调用以与第二个数据库中的名称(nume)关联

而不是 0, 0, 0, 0, 0, 0(写在 db 中),用 ', ' 分隔我想显示元素名称

示例:

 INSERT INTO `iteme` VALUES ('1', 'Name of item 1', .....');
INSERT INTO `iteme` VALUES ('2', 'Name of item 2', .......');
INSERT INTO `iteme` VALUES ('3', 'Name of item 3 ', .......');

并且

 INSERT INTO `alchimie` VALUES ('1', '100', 'Fusar ', '1', '1', '1, 2, 3');

它将出现在“obecte necesare”

Image 2 http://img59.imageshack.us/img59/1614/78551339.png

我希望这一次我终于能让你明白我想要做什么但失败了

最佳答案

首先,您需要在 iteme 表中有一列来引用 alchimie,在下面的示例中,我给它起了一个名称 alchimie_id

现在,LEFT JOINGROUP BYGROUP_CONCAT() 将通过查询实现这一目的

SELECT a.nume_potiune, 
a.nivel_potiune,
a.nivel_caracter,
i.obiecte_necesare
FROM alchimie a LEFT JOIN
(
SELECT alchimie_id,
GROUP_CONCAT(nume) obiecte_necesare
FROM iteme
GROUP BY alchimie_id
) i
ON a.id = i.alchimie_id

示例输出:

|  NUME_POTIUNE | NIVEL_POTIUNE | NIVEL_CARACTER |                          OBIECTE_NECESARE |----------------------------------------------------------------------------------------------|         Fusar |             1 |              1 | Name of item1,Name of item2,Name of item3 ||      Caracuda |             1 |              1 |                                    (null) || Tipar Chiscar |             2 |              1 |                                    (null) ||         Salau |             2 |              2 |                                    (null) |

这里是SQLFiddle 演示。

关于php mysql 数据库项名称从另一个数据库调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16989291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com