skip to Main Content

I have two different table in phpmyadmin. One is tbl_user and another is donate.

Now I want to take the column donation_date from tbl_user and all columns from the donate table. I want to join one column (donation_date) from tbl_user table and all cloumns from donate table, but dont know how to write the query.

In the below code I just wrote the query of donate table so how can I join the donation_date from the tbl_user.

Here is my details of two tables in phpmyadmin.

tbl_user table :

tbl_user table

tbl_user output

donate table:

donate table

donate output

$db = new PDO('mysql:host=localhost;dbname=mypro_bms','root','');
$statement = $db->prepare(
    "insert into donate(passport_ic,blood_group,blood_bag,)
     values(:passport_ic, :blood_group, :blood_bag)"
);

3

Answers


  1. I want to take the column donation_date from tbl_user and all columns from the donate table.

    Are you looking for… a simple JOIN between tables tbl_user and donate?

    From your schema images, it looks like column passport_IPC can be used to join the tables. Then you can choose which columns to return in the SELECT clause:

    SELECT u.donation_date, d.*
    FROM tbl_user u
    INNER JOIN donate d ON d.passport_IPC = u.passport_IPC
    
    Login or Signup to reply.
  2. Looks like Passport_IC is the common column between the two tables?

    You really should use be using numeric, indexed columns. But since you have a small DB, this should work:

    SELECT d.*, u.donation_date
    FROM donate d
    INNER JOIN tbl_user u ON u.Passport_IC = d.Passport_IC
    

    EDIT: You should give your a tbl_user table a primary key as well. Look into database normalization: https://en.wikipedia.org/wiki/Database_normalization

    Login or Signup to reply.
  3. As you said, you want to take the column donation_date from tbl_user so you will select just the column nedded tbl_user.donation_date ans use the following alias donation_date and to get all columns from the donate table you can use this trick donate.*, it gets all column

    SELECT 
        donate.* , tbl_user.donation_date as donation_date 
    FROM 
        tbl_user
    JOIN 
        donate ON donate.passport_IPC = tbl_user.passport_IPC
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search