我的数据库中有一个重复错误,当我输入2条记录时,我想使用SELECT命令通过终端显示它们并在两个表之间进行联接,它们显示4条记录。在这里我为no_servis和kode_pelanggan使用2个自动代码 上一个很好,当我更改数据库中的一列然后将其返回到原始列时。而是这样的错误
MariaDB [sijarvis]> SELECT * FROM tb_penerimaan;
| no_servis | tgl_terima | kode_pelanggan | nama_perangkat | model_perangkat | no_model | keluhan | kelengkapan | dp | status | solusi | harga | presentase |
| SER00000001 | 2019-01-25 | PE000001 | ASUS | Elitebook 8460p | CVT123 | projek | meh | 5000 | Di Servis | Instal Office | 150000 | 15 |
| SER00000002 | 2019-01-28 | PE000002 | ASUS | X453SA | asdasd | nsdklfnskslii | nflsfndklnskln | 10012 | Di Servis | | 0 | |
+-------------+------------+----------------+----------------+-----------------+----------+---------------+----------------+-------+-----------+---------------+--------+------------+
2 rows in set (0.00 sec)
MariaDB [sijarvis]> SELECT * FROM tb_pelanggan;
+----------------+-------+--------+-------------+--------+
| kode_pelanggan | nama | alamat | no_hp | member |
+----------------+-------+--------+-------------+--------+
| PE000001 | admin | Hatimu | 05628675848 | member |
| PE000002 | Anggi | balen | 987304209 | member |
+----------------+-------+--------+-------------+--------+
2 rows in set (0.00 sec)
MariaDB [sijarvis]> SELECT tb_penerimaan.* , tb_pelanggan.nama FROM tb_penerimaan, tb_pelanggan;
+-------------+------------+----------------+----------------+-----------------+----------+---------------+----------------+-------+-----------+---------------+--------+------------+-------+
| no_servis | tgl_terima | kode_pelanggan | nama_perangkat | model_perangkat | no_model | keluhan | kelengkapan | dp | status | solusi | harga | presentase | nama |
+-------------+------------+----------------+----------------+-----------------+----------+---------------+----------------+-------+-----------+---------------+--------+------------+-------+
| SER00000001 | 2019-01-25 | PE000001 | ASUS | Elitebook 8460p | CVT123 | projek | meh | 5000 | Di Servis | Instal Office | 150000 | 15 | admin |
| SER00000002 | 2019-01-28 | PE000002 | ASUS | X453SA | asdasd | nsdklfnskslii | nflsfndklnskln | 10012 | Di Servis | | 0 | | admin |
| SER00000001 | 2019-01-25 | PE000001 | ASUS | Elitebook 8460p | CVT123 | projek | meh | 5000 | Di Servis | Instal Office | 150000 | 15 | Anggi |
| SER00000002 | 2019-01-28 | PE000002 | ASUS | X453SA | asdasd | nsdklfnskslii | nflsfndklnskln | 10012 | Di Servis | | 0 | | Anggi |
+-------------+------------+----------------+----------------+-----------------+----------+---------------+----------------+-------+-----------+---------------+--------+------------+-------+
4 rows in set (0.00 sec)
为什么当我加入两个表时显示4条记录? 这是我从tambah_transaksi.php插入的查询
<?php
include('koneksi.php');
// Proses input data
$no_servis = $_POST['no_servis'];
$tgl_terima = $_POST['tgl_terima'];
$nama_perangkat = $_POST['nama_perangkat'];
$model_perangkat = $_POST['model_perangkat'];
$no_model = $_POST['no_model'];
$keluhan = $_POST['keluhan'];
$kelengkapan = $_POST['kelengkapan'];
$dp = $_POST['dp'];
$kode_pelanggan = $_POST['kode_pelanggan'];
$nama = $_POST['nama'];
$alamat = $_POST['alamat'];
$nohp = $_POST['no_hp'];
$member = $_POST['member'];
$hak_akses = $_POST['hak_akses'];
$password = $_POST['password'];
$status = $_POST['status'];
$solusi = $_POST['solusi'];
$presentase = $_POST['presentase'];
$harga = $_POST['harga'];
// query SQL untuk insert data
$query1 = "INSERT INTO tb_penerimaan VALUES('$no_servis','$tgl_terima','$kode_pelanggan','$nama_perangkat','$model_perangkat','$no_model','$keluhan','$kelengkapan','$dp','$status','$solusi','$harga','$presentase')";
$query2 = "INSERT INTO tb_pelanggan VALUES('$kode_pelanggan','$nama','$alamat','$nohp','$member')";
$query3 = "INSERT INTO login VALUES('$kode_pelanggan','$nama','$password','$hak_akses')";
$hasil = mysql_query($query1);
$hasil = mysql_query($query2);
$hasil = mysql_query($query3);
header("location: nota-masuk.php?no_servis='$no_servis'");
?>
答案 0 :(得分:1)
您需要加入
SELECT tb_penerimaan.* , tb_pelanggan.nama
FROM tb_penerimaan
INNER JOIN tb_pelanggan ON tb_penerimaan.tb_penerimaan = tb_pelanggan.kode_pelanggan ;
如果不添加联接条件,则可以从各行获得笛卡尔积(对于一个表中的每一行,请选择另一行的所有行)..如果建立联接,则建立仅选择匹配行的关系
答案 1 :(得分:0)
SELECT tb_penerimaan.* , tb_pelanggan.nama FROM tb_penerimaan inner join
tb_pelanggan ON tb_pelanggan.kode_pelanggan=tb_penerimaan.kode_pelanggan;
in this two table, this is common id (kode_pelanggan) so when we join the to
table use ON condition to match this then you get a unique id you don't get
duplicates
答案 2 :(得分:0)
但是当我在tb_penerimaan中更新“ SER00000001”时,出现错误Duplicate entry 'SER00000001' for key 'PRIMARY'
,并且在表tb_penerimaan中只有两条记录
SELECT * FROM tb_penerimaan;
+-------------+------------+----------------+----------------+-----------------+----------+---------------+----------------+-------+-----------+---------------+--------+------------+
| no_servis | tgl_terima | kode_pelanggan | nama_perangkat | model_perangkat | no_model | keluhan | kelengkapan | dp | status | solusi | harga | presentase |
+-------------+------------+----------------+----------------+-----------------+----------+---------------+----------------+-------+-----------+---------------+--------+------------+
| SER00000001 | 2019-01-25 | PE000001 | ASUS | Elitebook 8460p | CVT123 | projek | meh | 5000 | Di Servis | Instal Office | 150000 | 15 |
| SER00000002 | 2019-01-28 | PE000002 | ASUS | X453SA | asdasd | nsdklfnskslii | nflsfndklnskln | 10012 | Di Servis |
| 0 | |
+-------------+------------+----------------+----------------+-----------------+----------+---------------+----------------+-------+-----------+---------------+--------+------------+