[英]Get foreign id in relational mysql tables
I have two mysql tables like this: 我有两个这样的mysql表:
table 'names':
{
name varchar(25),
date_id,
FOREIGN KEY (date_id) REFERENCES dates(date_id)
}
table 'dates'
{
date datetime,
date_id INT PRIMARY KEY AUTO_INCREMENT
}
I need to SELECT all names with specific date. 我需要选择所有带有特定日期的名称。 Currently i make two requests.
目前,我提出两个要求。 First one to get the id of date.
第一个获取日期的ID。 And second one to get names.
第二个获得名字。 Can i make only one request somehow?
我可以只发出一个请求吗?
$sql = "SELECT date_id WHERE date='$date'"; // $date is safe variable
$sql = "SELECT name FROM names WHERE date_id='$id'";
Try: 尝试:
SELECT `name`, `date_id`
FROM `dates`
INNER JOIN `names`
ON `names`.`date_id` = `dates`.`date_id`
WHERE `dates`.`date` = '$date';
Yes, you can with an INNER JOIN : 是的,您可以使用INNER JOIN :
$sql = "SELECT names.name" .
" FROM names INNER JOIN dates ON names.date_id = dates.date_id" .
" WHERE dates.date='$date'";
select p1.* from names p1 inner join dates p2 on p1.date_id =p2.date_id
You can use simple JOIN to select the data as: 您可以使用简单的JOIN选择数据,如下所示:
SELECT `name`, `date_id`
FROM `dates`
INNER JOIN `names`
ON `names`.`date_id` = `dates`.`date_id`
WHERE `dates`.`date` = 'your_date_variable';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.