好的,我是mySQL的新手。对不起,这是一个非常新手的问题。基本上我有两个表,员工和密钥 内容如下:
关联: ID, 部门, 关联, DATE_ADDED
键: ID, KEY_NAME, DATE_ADDED,
我的下拉列表如下:
<?php
mysql_connect('hostname', 'user', 'Password');
mysql_select_db('log');
$key_fetch = "SELECT `associates`.`department`,`associates`.`associate`,`keys`.`key_name` FROM associates , `keys` ORDER BY `key_name` DESC";
$results = mysql_query($key_fetch);
echo "<select name='key_name' size='5'>";
while ($row = mysql_fetch_array($results)) {
echo "<option value='" . $row['key_name'] . "'>" . $row['key_name'] . "</option>";
}
echo "</select>";
?>
问题是我只有5个键,而且我有10个同事,这会在我的下拉列表中创建重复项,我无法使用SELECT DISTINCT修复它,而且我不太确定还有什么可以尝试
答案 0 :(得分:0)
根据以上Q评论可视化笛卡尔积。
create table t1
( id int auto_increment primary key,
stuff1 varchar(50) not null
);
insert t1 (stuff1) values ('111.1'),('111.2'),('111.3');
create table t2
( id int auto_increment primary key,
stuff2 varchar(50) not null
);
insert t2 (stuff2) values ('222.1'),('222.2'),('222.3');
select t1.id,t1.stuff1,t2.stuff2
from t1
join t2
on t2.id=t1.id;
+----+--------+--------+
| id | stuff1 | stuff2 |
+----+--------+--------+
| 1 | 111.1 | 222.1 |
| 2 | 111.2 | 222.2 |
| 3 | 111.3 | 222.3 |
+----+--------+--------+
select t1.id,t1.stuff1,t2.stuff2
from t1,t2;
+----+--------+--------+
| id | stuff1 | stuff2 |
+----+--------+--------+
| 1 | 111.1 | 222.1 |
| 2 | 111.2 | 222.1 |
| 3 | 111.3 | 222.1 |
| 1 | 111.1 | 222.2 |
| 2 | 111.2 | 222.2 |
| 3 | 111.3 | 222.2 |
| 1 | 111.1 | 222.3 |
| 2 | 111.2 | 222.3 |
| 3 | 111.3 | 222.3 |
+----+--------+--------+
9 rows in set (0.00 sec)
select t1.id,t1.stuff1,t2.stuff2
from t1 cross join t2
所以,你看来,你的输出就像B,50行。