dpqg86714 2014-05-06 13:34
浏览 28
已采纳

查询与另一个表相关的信息

I have two tables - Utilizadores and Competencias.

NOTE: Utilizadores = Users And Competencias = Skills

Each user can have 1 or more Skills.

I need to print them in a dropdown menu, where, for each skill, it shows the user that has that skill.

So, SKILLS->USER THAT HAS THE SKILL (The visual is all done, but it doesn't work properly - Explained next)

Here is the first query:

$query2 = mysql_query("SELECT DISTINCT competencias, id_user FROM ce");
$ut = array();
while ($result = mysql_fetch_array($query2)) $ut[] = $result;
foreach ($ut as $u){
    $id_user=$u[1];
}

(Nevermind the number 2 on the query2.) This query searches in the table "Skills" the id of the user that contains that specific skill. Again, A user can have more than just 1 skill.

Second Query:

$query3 = mysql_query("SELECT `id`, `nome` FROM utilizadores WHERE id='$id_user'");
$utt = array();
while ($result2 = mysql_fetch_array($query3)) $utt[] = $result2;
foreach ($utt as $utilizador){
    $nomeuser=$utilizador[1];
}

This Second Query searched on the table "Users" for the names of the users

Anf finaly, the third part:

foreach ($ut as $hu){
echo "<li class='has-sub'><a><span>" .$hu[0]. "</span></a>";
    echo "<ul><a href='php/curriculo.php?nomeut=$nomeuser'>". $nomeuser. "</a></ul>";
}

Now the problem I'm Having: When the mouse overs the skill present on the dropdown menu, it shows a sub-dropdown showing only the name of the user that contains the skill. But it always shows me the same name.

In the database, Several Skills belong to Several Users, but, on the sub-dropdown, it shows the name of ONE person only, instead of the ones corresponding to their skill.

Let's say:
John has a skill "Sharepoint"
Smith has a skill "Whatever"

When i pass the mouse on "Sharepoint" ->John
When i pass the mouse on "Whatever" -> John - When it really corresponds to "Smith".

I am sorry for the realy long problem, and for my awful english.

Any help is appreciated
Thanks :)

  • 写回答

2条回答 默认 最新

  • dsfovbm931034814 2014-05-06 14:07
    关注

    Since it isn't completely clear to me what info you want from your database, I'll describe you some options. You can process the results with you PHP code. If you need more, just comment and I'll update my answer.

    Let say that you got your tables populated like this:

    users
    user_id | username
    1       | John
    2       | Matt
    3       | Rob
    
    skills
    user_id | skill
    1       | PHP
    1       | SQL
    1       | HTML
    2       | PHP
    4       | HTML
    

    When using a left join, all the rows of the left table will be returned. If there is no match in the right table, it will return NULL. This query

    SELECT u.username, s.skill 
    FROM users AS u 
    LEFT JOIN skills as s
    ON u.user_id=s.user_id
    

    Will return:

    John    PHP
    John    SQL
    John    HTML
    Matt    PHP
    Rob     NULL
    

    Using a right join, will return all the rows from the right table, giving a NULL if there is no match in the left table

    SELECT u.username, s.skill
    FROM users AS u
    RIGHT JOIN skills AS s 
    ON u.user_id = s.user_id
    

    Will return:

    John    PHP
    John    SQL
    John    HTML
    Matt    PHP
    NULL    HTML
    

    Using the inner join, all rows that match in both tables will return. So this query

    SELECT u.username, s.skill
    FROM users AS u
    RIGHT JOIN skills AS s 
    ON u.user_id = s.user_id
    

    will return

    John    PHP
    John    SQL
    John    HTML
    Matt    PHP
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作