Display records if first character of name is same as last character in mysql



     Summary:

    In this tutorial we will learn display records if first character of name is same as last character in mysql and also display records if first character of the first name is same as first character of the last name. To solve this we create one table in mysql and start our demonstration.

    Prerequisite:    

    Now we are going to create one table named staff using the below query.

    mysql> create table staff ( deptno int ,firstname varchar(20),lastname varchar(20),sal int);
    Query OK, 0 rows affected (0.07 sec)

    Now describe the staff table which we have just created.

    describe table in mysql



    Now we are going to insert some records into the staff table. To do this we need to execute the below query.

    mysql> insert into staff (deptno,firstname,lastname,sal) values ( 1, 'ram','verma',2000);
    Query OK, 1 row affected (0.00 sec)

    mysql> insert into staff (deptno,firstname,lastname,sal) values ( 1, 'kartik','sharma',5000);
    Query OK, 1 row affected (0.00 sec)

    mysql> insert into staff (deptno,firstname,lastname,sal) values ( 1, 'kartik','khan',9000);
    Query OK, 1 row affected (0.00 sec)

    mysql> insert into staff (deptno,firstname,lastname,sal) values ( 1, 'aftab','ali',9000);
    Query OK, 1 row affected (0.00 sec)

    Display records if first character of name is same as last character in mysql

    To display first any  number of records we will use  left() function and to display last any number of records we will use right () function.

    Now we will fetch all records from our table using the below query.

    mysql> select * from staff;



    Now we want to  display records if first character of name is same as last character in mysql using the below query.

    mysql> select * from staff where left(firstname,1)=right(firstname,1);

    Display records if first character of name is same as last character in mysql

    Display records if first character of the first name is same as first character of the last name.

    Now we are going to display records if first character of the first name is same as first character of the last name. To do this we need to execute the below query.

    mysql> select * from staff where left(firstname,1)=left(lastname,1);

    Display records if first character of the first name is same as first character of the last name


    Conclusion 

    In this tutorial we have learneddisplay records if first character of name is same as last character in mysql and also display records if first character of the first name is same as first character of the last name.



    SHARE

    Admin

    • Image
    • Image
    • Image
    • Image
    • Image
      Blogger Comment
      Facebook Comment

    0 comments:

    Post a Comment