普通网友 2013-07-30 04:58
浏览 120
已采纳

SELECT * FROM有两个不同的表[重复]

This question already has an answer here:

I've been fiddling with this for bit and tried a couple different ways to combine both SELECT FROM statements, but I can't get it to work where I can call on fields from two tables within the same database. It works find if I remove either one. I tried to combine them using UNION (not sure if that's the correct thing), but couldn't get it to work.

Here's what I got so far with them separated:

$id = $_POST['id'];

$edit = $db->prepare("SELECT * FROM contacts WHERE id = (?)");
$edit->bind_param('s', $id);
$edit->execute();
$edit->bind_result($id, $firstName, $lastName, $email, $phone, $category);

$categories = $db->prepare("SELECT * FROM category");
$categories->execute();
$categories->bind_result($cateid, $setcategory);

echo $firstName;

while ($categories->fetch()) {
    echo $setcategory;
}

I'm new to all of this. I'd be grateful if someone could point me in the right direction. Thanks!

</div>
  • 写回答

2条回答 默认 最新

  • doumei4964 2013-07-30 05:08
    关注

    if the two tables have a common field then you can do something like

    "SELECT A.*, B.*
     FROM contacts AS A
     JOIN category AS B
     ON A.primary = B.foreign
     WHERE A.id = (?)"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题