我有两个表,我希望显示所有内容,但是按某个字段将内容分组在一起 - job_id字段:
两个表:
- job_experience
job_id(主键)
JOB_NAME
USER_ID
- job_skills
skill_id(主键)
job_id
skill_name
这应该通过匹配两个表job_ids来生成:
职位名称1 技巧1
技巧2
工作名称2
技巧1
我目前有经验表的工作循环,但我无法确定如何包含job_skills并通过job_id将它们组合在一起。我尝试了一个嵌套循环,其SQL查询侧重于匹配job_ids,但这不起作用。
<?php
$experience_sql =mysql_query("select e.job_id, e.job_name,from job_experience e where e.user_id='$user_id_session'", $connection);
while ($row = mysql_fetch_assoc($experience_sql)) {
?>
<textarea cols="50" rows="10">
<?php
echo $row['job_name']." "."\n";
?>
</textarea>
<?php
}
?>
答案 0 :(得分:0)
可能你正在寻找像这样的声明
select e.job_id, e.job_name,s.skill_name from job_experience e, job_skills s where e.job_id=s.job_id and e.user_id='$user_id_session'
或
select e.job_id, e.job_name,s.skill_name from job_experience e inner join job_skills s on e.job_id=s.job_id where e.user_id='$user_id_session'
答案 1 :(得分:0)
您可以使用以下查询将与作业相关的所有技能作为逗号分隔列表:
SELECT e.job_id, e.job_name, GROUP_CONCAT(s.job_skills)
FROM job_experience e
LEFT JOIN job_skills s ON e.job_id = s.job_id
GROUP BY e.job_id
答案 2 :(得分:0)
<?php
$experience_sql = mysql_query("select e.job_id, e.job_name, s.skill_name from job_experience e inner join job_skills s on e.job_id=s.job_id where e.user_id='$user_id_session' ",$connection);
while ($row = mysql_fetch_assoc($experience_sql)){
?>
<textarea cols="50" rows="10">
<?php
echo $row['job_name']." "."\n";
echo $row['skill_name']." "."\n";
?>
</textarea>
<?php
}
?>