 |
ไม่ให้ข้อมูลขึ้นซ้ำหลายเรคคอร์ด โดยการ JOIN ตารางมีวิธีแก้ไขไหมครับ |
|
 |
|
|
 |
 |
|
ใช้ distinct
|
 |
 |
 |
 |
Date :
2011-08-15 14:19:21 |
By :
Dragons_first |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เหมือนเดิมครับ
SELECT DISTINCT(tel.id_detail),tel.tel,detail.ThaiName FROM detail LEFT JOIN tel ON detail.id_detail=tel.id_detail WHERE detail.Enable='T' AND detail.IsActive='T'
|
 |
 |
 |
 |
Date :
2011-08-15 14:56:45 |
By :
MyZeus |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
SELECT DISTINCT(tel.id_detail),tel.tel,detail.ThaiName FROM detail INNER JOIN tel ON detail.id_detail=tel.id_detail WHERE detail.Enable='T' AND detail.IsActive='T'
เปลี่ยนจาก LEFT เป็น INNER ได้ไหม
|
 |
 |
 |
 |
Date :
2011-08-15 15:04:15 |
By :
ไวยวิทย์ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เหมือนเดิมเลยครับ
|
 |
 |
 |
 |
Date :
2011-08-15 15:13:11 |
By :
MyZeus |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
SELECT detail.ThaiName, tel.tel FROM detail INNER JOIN tel ON detail.id_detail=tel.id_detail WHERE detail.Enable='T' AND detail.IsActive='T' GROUP BY detail.ThaiName
แบบนี้ล่ะ
|
 |
 |
 |
 |
Date :
2011-08-15 15:25:10 |
By :
ไวยวิทย์ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
error ครับ มันบอกต้องใส่ tel.tel ที่ group by ด้วย
SELECT detail.ThaiName, tel.tel FROM detail INNER JOIN tel ON detail.id_detail=tel.id_detail WHERE detail.Enable='T' AND detail.IsActive='T' GROUP BY detail.ThaiName,tel.tel
ผมก็ใส่ไปละ และแล้วมันก็เหมือนเดิมครับ T_T
|
 |
 |
 |
 |
Date :
2011-08-15 15:35:31 |
By :
MyZeus |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตาราง detail
Code (PHP)
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for detail
-- ----------------------------
DROP TABLE IF EXISTS `detail`;
CREATE TABLE `detail` (
`id_detail` int(11) NOT NULL auto_increment,
`thainame` text,
PRIMARY KEY (`id_detail`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of detail
-- ----------------------------
INSERT INTO `detail` VALUES ('1', 'AAA');
INSERT INTO `detail` VALUES ('2', 'BBB');
INSERT INTO `detail` VALUES ('3', 'CCC');
INSERT INTO `detail` VALUES ('4', 'DDD');
INSERT INTO `detail` VALUES ('5', 'EEE');
ตาราง tel
Code (PHP)
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for tel
-- ----------------------------
DROP TABLE IF EXISTS `tel`;
CREATE TABLE `tel` (
`id_tel` int(10) NOT NULL auto_increment,
`tel` text,
`id_detail` int(10) default NULL,
PRIMARY KEY (`id_tel`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of tel
-- ----------------------------
INSERT INTO `tel` VALUES ('1', '085-111-2222', '1');
INSERT INTO `tel` VALUES ('2', '084-222-3333', '1');
INSERT INTO `tel` VALUES ('3', '085-333-4444', '1');
INSERT INTO `tel` VALUES ('4', '086-234-5678', '2');
INSERT INTO `tel` VALUES ('5', '088-999-5566', '1');
SQL ที่ผมลองเขียน มันก็ขึ้นชื่อร้านเดียว ถึงแม้มันจะมีหลายเบอร์โทร
SELECT detail.thainame, tel.tel FROM detail INNER JOIN tel ON detail.id_detail=tel.id_detail GROUP BY thainame
|
 |
 |
 |
 |
Date :
2011-08-15 15:46:45 |
By :
ไวยวิทย์ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ok จบป๊ะ....
|
 |
 |
 |
 |
Date :
2011-08-15 16:02:25 |
By :
Dragons_first |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ของผมมันไม่ได้อ่าคับ

|
 |
 |
 |
 |
Date :
2011-08-15 16:14:58 |
By :
MyZeus |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for detail
-- ----------------------------
DROP TABLE IF EXISTS `detail`;
CREATE TABLE `detail` (
`id_detail` int(11) NOT NULL auto_increment,
`thainame` text,
`enable` text,
`active` text,
PRIMARY KEY (`id_detail`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of detail
-- ----------------------------
INSERT INTO `detail` VALUES ('1', 'AAA', 'T', 'T');
INSERT INTO `detail` VALUES ('2', 'BBB', 'T', 'T');
INSERT INTO `detail` VALUES ('3', 'CCC', 'N', 'N');
INSERT INTO `detail` VALUES ('4', 'DDD', null, null);
INSERT INTO `detail` VALUES ('5', 'EEE', null, null);
Code (PHP)
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for tel
-- ----------------------------
DROP TABLE IF EXISTS `tel`;
CREATE TABLE `tel` (
`id_tel` int(10) NOT NULL auto_increment,
`tel` text,
`id_detail` int(10) default NULL,
PRIMARY KEY (`id_tel`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of tel
-- ----------------------------
INSERT INTO `tel` VALUES ('1', '085-111-2222', '1');
INSERT INTO `tel` VALUES ('2', '084-222-3333', '1');
INSERT INTO `tel` VALUES ('3', '085-333-4444', '1');
INSERT INTO `tel` VALUES ('4', '086-234-5678', '2');
INSERT INTO `tel` VALUES ('5', '088-999-5566', '1');
INSERT INTO `tel` VALUES ('6', '087-555-1111', '3');
INSERT INTO `tel` VALUES ('7', '082-111-3333', '3');
มีฟิว detail.enable และ detail.active
SELECT detail.thainame, tel.tel FROM detail INNER JOIN tel ON detail.id_detail=tel.id_detail WHERE detail.enable = 'T' AND detail.active = 'T' GROUP BY thainame
ไม่ต้องเอา tel มา GROUP BY ด้วยได้ไหม
|
 |
 |
 |
 |
Date :
2011-08-15 16:44:36 |
By :
ไวยวิทย์ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แต่ของผมถ้า select ฟิวด์อะไรแล้ว ต้อง group by ฟิวด์นั้นด้วยครับ มันจึงต้องใส่ tel.tel ไปด้วย
ตอนนี้ผมทำได้แล้ว
Code (PHP)
select d.thainame, (select top 1 tel from tel where id_detail = d.id_detail) from detail d
แต่ติดตรงที่ผมไป echo ใน php ไม่ออกครับ
|
 |
 |
 |
 |
Date :
2011-08-16 20:01:05 |
By :
MyZeus |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับผม
|
 |
 |
 |
 |
Date :
2011-10-15 22:08:55 |
By :
เหน่ง |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|