简体   繁体   English

从表2获取信息以及从表1获取信息

[英]getting info from table 2 with the info from table 1

I'm not experienced with SQl (and also not THAT much with php) 我没有SQl的经验(也没有php的经验)
I'm currently am getting my info from a table from my database and putting it on my Website. 我目前正在从数据库中的表中获取信息,并将其放在我的网站上。 Now I'm trying to get info from another table trough data from my 1st table trough $sql = "SELECT * FROM patients"; 现在,我正在尝试从我的第一个表格槽中的另一个表格槽数据中获取信息$sql = "SELECT * FROM patients";
Example: In my current syntax row 3 called "species_id" is a '1'. 示例:在我当前的语法中,名为“ species_id”的第3行为“ 1”。 In another table the ID is 1 and species = "cat". 在另一个表中,ID为1,种类=“ cat”。 I want cat in my row where I drop the info from the first table. 我要在行中放入第一张表格中的信息的猫。
in theory I want something like 理论上我想要类似

$sql = "SELECT * FROM patients SELECT species_description 
        FROM species 
        WHERE species_id=species_id;";


(wich obviously doesnt work but I wanted you to have a better idea) How do I correctly do this (the best way)? (这显然不起作用,但我希望您有一个更好的主意)如何正确执行此操作(最佳方法)?

assuming you have species_id in your patientes table and this is the relation value for the id in species you could buil a join 假设您的Patientes表中有species_id,这是您可以建立联接的物种中id的关联值

    $sql = "SELECT patients.* , species.species_description
            FROM patients
            INNER JOIN species on patients.species_id=species.id;";

thanks everyone for an answer, i fixed it using this code 谢谢大家的回答,我使用此代码修复了它

$sql = "SELECT patients.* , species.species_description
    FROM patients
    INNER JOIN species on patients.species_id = species.species_id;";

thanks to the answer from @scaisEdge (thank you!) and w3schools page on how to use INNER JOIN (never knew about that before) thanks! 感谢@scaisEdge (谢谢!)和w3schools页面上有关如何使用INNER JOIN的答案(以前从未了解过),谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM