4 Replies - 340 Views - Last Post: 07 October 2018 - 04:19 AM Rate Topic: -----

#1 akane96   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 133
  • Joined: 21-March 17

Don't understand the query

Posted 06 October 2018 - 11:38 AM

hello i just have one question about a query used in a codeigniter project . I will comment the line I don't understand. Thanks for the help.
    public function getLoginPwd($login,$pwd){
     return   $this->db->select("u.*")/* What does the "u.*" inside the select means? Because  When I checked in the database there was no table named u but i have there one named User */
      ->from($this->getTableName()."  u")
      ->where("login",$login)
      ->where("pwd",$pwd)
      ->get()
      ->result();
  }





Is This A Good Question/Topic? 0
  • +

Replies To: Don't understand the query

#2 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7471
  • View blog
  • Posts: 15,493
  • Joined: 16-October 07

Re: Don't understand the query

Posted 06 October 2018 - 11:48 AM

Well, that's some craptastically ugly DSL. PHP?

Assuming this awkward abstraction resolves to SQL, then the u. becomes an alias to the table name supplied by $this->getTableName().

So:
select u.*
    from some_table_name u
    where login=? and pwd=?


This makes a degree of sense if you don't know the table name going in. Though, with the *, the alias seems silly.
Was This Post Helpful? 0
  • +
  • -

#3 akane96   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 133
  • Joined: 21-March 17

Re: Don't understand the query

Posted 06 October 2018 - 12:43 PM

Yeah actually i found out that he defines the function getTableName like this
   public function getTableName(){

          return "user";
    }



What's the alias for? Is that some way to do a more secure query or does it allow to code more faster ?

Yeah actually i found out that he defines the function getTableName like this
   public function getTableName(){

          return "user";
    }



What's the alias for? Is that some way to do a more secure query or does it allow to code more faster ?
Was This Post Helpful? 0
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7471
  • View blog
  • Posts: 15,493
  • Joined: 16-October 07

Re: Don't understand the query

Posted 06 October 2018 - 02:25 PM

An alias is just a label for a table or result set.

For tables, it's usually just a shorter name e.g.
select u.name as username, g.name as groupname
    from user_group ug
        join user u on u.user_id=ug.user_id
        join group g on g.group_id=ug.group_id



For subqueries, an alias is sometimes a necessity.
select a.name as groupname, b.users_in_group
    from group a
        join (select group_id, count(*) as users_in_group from user_group group by group_id) b
            on a.group_id=b.group_id


Was This Post Helpful? 0
  • +
  • -

#5 akane96   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 133
  • Joined: 21-March 17

Re: Don't understand the query

Posted 07 October 2018 - 04:19 AM

Alright thank you for your help have a wonderful week-end.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1