简体   繁体   中英

selecting multiple rows issue in mysql

I am beginner in sql. I have two tables users and installments

CREATE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(20) NOT NULL,
  `password` varchar(10) NOT NULL,
  `father_name` varchar(20) NOT NULL,
  `phone` varchar(20) NOT NULL,
  `cnic` varchar(20) NOT NULL,
  `email` varchar(100) NOT NULL,
  `address` varchar(100) NOT NULL,
  `introducer` varchar(100) NOT NULL,
  `date` date DEFAULT NULL,
  `reg_number` varchar(100) DEFAULT NULL,
  `installment` int(100) NOT NULL,
  `user_level` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `cnic` (`cnic`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

INSERT INTO `users` (`id`, `user_name`, `password`, `father_name`, `phone`, `cnic`, `email`, `address`, `introducer`, `date`, `reg_number`, `installment`, `user_level`) VALUES
(2, 'qaser', 'Qaser1', 'zamarrud', '0312546879', '37406-3140185-1', 'tariq_kareem@yahoo.com', 'street # 6', 'rizwan', '2014-08-20', 'E-002', 3000, 0);

and

CREATE `installments` (
      `installment_id` int(11) NOT NULL AUTO_INCREMENT,
      `month` date DEFAULT NULL,
      `prv_arrear` int(100) NOT NULL,
      `amount` int(100) NOT NULL,
      `total` int(100) NOT NULL,
      `receive` int(100) NOT NULL,
      `arrear` int(100) NOT NULL,
      `fk_users_id` int(11) NOT NULL,
      PRIMARY KEY (`installment_id`),
      KEY `fk_users_id` (`fk_users_id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

INSERT INTO `installments` (`installment_id`, `month`, `prv_arrear`, `amount`, `total`, `receive`, `arrear`) VALUES
(2, '2014-08-20', 2000, 2500, 4500, 3000, 1500);

when I run the following query I get the first table's record correctly but the second table's record showing NULL values

    SELECT * FROM users
LEFT JOIN installments
ON users.id=installments.installment_id
WHERE users.cnic='37406-3140185-1';

Is there anything missing in the above query or is there another way to get the record from both tables simultaneously

id  user_name   password    father_name     phone   cnic                  email                    address  introducer      date       reg_number   installment     user_level  installment_id  month   prv_arrear  amount  total   receive     arrear  fk_users_id
2   qaser       Qaser1      zamarrud    0312546879  37406-3140185-1     tariq_kareem@yahoo.com  street # 6  rizwan        2014-08-20    s-001         3000           0          NULL            NULL    NULL        NULL    NULL    NULL        NULL    NULL

I am also using the following query to inserting the record for getting primary key value and insert into foreign key

INSERT INTO `installments`(`id`, `month`, `prv_arrear`, `amount`, `total`, `receive`, `arrear`, fk_users_id)
SELECT NULL,now(),1000,2500,3500,3000,500, id
  FROM users
 WHERE cnic = '37406-3140190-1'

please help me thanks in advance and sorry if there is something wrong in my question because I am new in sql.

I suspect the right join condition is on fk_users_id . So this might do what you want:

SELECT *
FROM users u LEFT JOIN
     installments i
     ON u.id = i.fk_users_id
WHERE u.cnic = '37406-3140185-1';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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