these are the tables

and it results
我在表it_courses字段course_branch的第一行上有两个值。当我将分支表与it_courses表连接时,结果是这样的。它不会在it_coursestable中输入的第一行中显示第二个值


  查询:


SELECT branch.branch_name
FROM `branch`
JOIN `it_courses` ON it_courses.course_branch = branch.branch_id

最佳答案

您可以尝试以下数据库结构并查询:


  表:


tbl_it_courses(id-PK,cource_name,branch_id-FK)

tbl_branch(ID-PK,branch_id,branch_name)


  表格:tbl_it_courses


CREATE TABLE tbl_it_courses (
    id INT NOT NULL AUTO_INCREMENT,
    cource_name VARCHAR(70) NOT NULL,
    branch_id VARCHAR(70) DEFAULT NULL,
    PRIMARY KEY(id)
);

INSERT INTO `tbl_it_courses` (`Id`, `cource_name`, `branch_id`) VALUES (NULL, 'PHP', '1,4'),(NULL, '.NET', '3'), (NULL, 'CCNA', '3');



  表格:tbl_branch


CREATE TABLE tbl_branch (
    id INT NOT NULL AUTO_INCREMENT,
    branch_id VARCHAR(70) DEFAULT NULL,
    branch_name VARCHAR(70) NOT NULL,
    PRIMARY KEY(id)
);

INSERT INTO `tbl_branch` (`Id`, `branch_id`, `branch_name`) VALUES (NULL, 'Ern', 'brn92b224'),(NULL, 'Klm', 'brnaf3650'), (NULL, 'Tvm', 'brn272493');



  查询:


SELECT tbl_it_courses.cource_name, (SELECT GROUP_CONCAT(tbl_branch.branch_name) FROM tbl_branch WHERE FIND_IN_SET(tbl_branch.id,tbl_it_courses.branch_id) > 0)  as branch_name
FROM tbl_it_courses


希望对您有帮助。

关于php - 我在表it_courses字段course_branch的第一行上有两个值。当我将分支表与it_courses表联接时,结果只有一个值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47278659/

10-13 08:15