简体   繁体   English

从2个表格中获取信息

[英]Getting info from 2 tables

Having trouble with some select statements. 在某些选择语句上遇到麻烦。

I have 2 tables. 我有2张桌子。 sms_log and sms_messages. sms_log和sms_messages。

sms_log : sms_log:

id
message_id
user_id

sms_messages: sms_messages:

message_id
admin_id
message
date_sent
date_delivered

I want to get the sms_message data where sms_log.user_id = $id 我想获取sms_message数据,其中sms_log.user_id = $ id

How do i do it, im a bit stuck. 我该怎么做,即时通讯有点卡住。 Also, how will i access it through php, exactly the same as a standard query, or with the table name in front of the cell : 另外,我将如何通过php访问它,与标准查询完全相同,还是在单元格前面使用表名:

    `
echo $myrow['sms_messages.message'];

OR 要么

echo $myrow['message'];

thankyou ! 谢谢 !

First, you will need to perform an inner join on the SMS_MESSAGe and SMS_LOG tables on messageId. 首先,您将需要对messageId上的SMS_MESSAGe和SMS_LOG表执行内部联接。 This will look something like this: 看起来像这样:

SELECT m.Message 
FROM sms_Messages me
INNER JOIN sms_Log l ON l.Message_Id = m.Message_Id
Where l.user_Id = $id;

At this point, in your myrow variable, you'd access the message like: 此时,在myrow变量中,您将访问以下消息:

echo $myrow['message'];
select m.message as message
, m.date_sent as date_sent
FROM sms_messages as m
LEFT JOIN sms_log as l
ON m.message_id = l.message_id
WHERE l.user_id = $id

You'd still be able to access the array as you'd expect: 您仍然可以按预期访问阵列:

echo $arr['message'] ' sent on ' $arr['date_sent'] ;

Despite Stephen's brave attempt, it is impossible to determine from the information you provided how to join the two tables. 尽管Stephen做出了勇敢的尝试,但无法从您提供的信息中确定如何将两个表连接在一起。 The fact that there are columns in each table called message_id implies that it might be a foreign key in one and not the other. 每个表中都有称为message_id的列这一事实意味着它可能是一个外键,而不是另一个。

Are there any primary keys declared for the tables? 是否为表声明了任何主键? any unique indexes? 有唯一索引吗?

Is this your own data design or someone elses? 这是您自己的数据设计还是其他人? What are the data types for the columns? 列的数据类型是什么? What DBMS are you using? 您正在使用什么DBMS? Are any of the columns populated from a sequence generator or mysql auto-increment? 是否从序列生成器或mysql自动增量中填充了任何列?

What do these tables actually signify (OK SMS messages - I get that, but are they just incoming? 这些表实际上表示什么(好的SMS消息-我明白了,但是它们只是传入的吗?

C. C。

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

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