Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. MySQL

MySQL

Scheduled Pinned Locked Moved Database
databasemysqlsysadminhelpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    klaydze
    wrote on last edited by
    #1

    Hi All, Can someone explain why MySql treat the two sql statement below different? First Query (without inner join)

    MySqlConnection conn = new MySqlConnection("server=localhost;database=db_test;uid=root;password=1234567");
    MySqlCommand dbCommand = new MySqlCommand("select * from useraccess_privileges", conn);
    dbCommand.CommandType = CommandType.Text;
    if (dbConn.DBConnect.State == ConnectionState.Closed)
    dbConn.DBConnect.Open();
    DataTable dtData = new DataTable();
    dtData.Load(dbCommand.ExecuteReader());

    Result: dtData will return 7 row count

    Second Query (with inner join)

    MySqlConnection conn = new MySqlConnection("server=localhost;database=db_test;uid=root;password=1234567");
    MySqlCommand dbCommand = new MySqlCommand("select t1.*, t2.MenuName from user_access t1 inner join useraccess_privileges t2 on t1.UserName = t2.UserName", conn);
    dbCommand.CommandType = CommandType.Text;
    if (dbConn.DBConnect.State == ConnectionState.Closed)
    dbConn.DBConnect.Open();
    DataTable dtData = new DataTable();
    dtData.Load(dbCommand.ExecuteReader());

    Result: dtData will return 1 row count

    Second query should return 7 rows also since i just reference it to other table to get the full name of the specific username. Thanks

    if(you type your code here) { Messagebox.Show("You help me a lot!"); } else { You help me = null; }

    B 1 Reply Last reply
    0
    • K klaydze

      Hi All, Can someone explain why MySql treat the two sql statement below different? First Query (without inner join)

      MySqlConnection conn = new MySqlConnection("server=localhost;database=db_test;uid=root;password=1234567");
      MySqlCommand dbCommand = new MySqlCommand("select * from useraccess_privileges", conn);
      dbCommand.CommandType = CommandType.Text;
      if (dbConn.DBConnect.State == ConnectionState.Closed)
      dbConn.DBConnect.Open();
      DataTable dtData = new DataTable();
      dtData.Load(dbCommand.ExecuteReader());

      Result: dtData will return 7 row count

      Second Query (with inner join)

      MySqlConnection conn = new MySqlConnection("server=localhost;database=db_test;uid=root;password=1234567");
      MySqlCommand dbCommand = new MySqlCommand("select t1.*, t2.MenuName from user_access t1 inner join useraccess_privileges t2 on t1.UserName = t2.UserName", conn);
      dbCommand.CommandType = CommandType.Text;
      if (dbConn.DBConnect.State == ConnectionState.Closed)
      dbConn.DBConnect.Open();
      DataTable dtData = new DataTable();
      dtData.Load(dbCommand.ExecuteReader());

      Result: dtData will return 1 row count

      Second query should return 7 rows also since i just reference it to other table to get the full name of the specific username. Thanks

      if(you type your code here) { Messagebox.Show("You help me a lot!"); } else { You help me = null; }

      B Offline
      B Offline
      Bernhard Hiller
      wrote on last edited by
      #2

      Because user_access contains only one row matching useraccess_privileges. useraccess_privileges has 7 rows, user_access has 1 or more rows. Try a LEFT JOIN instead:

      SELECT useraccess_privileges.MenuName, user_access.*
      FROM useraccess_privileges
      LEFT JOIN user_access
      ON user_access.UserName=useraccess_privileges.UserName

      and you'll get again 7 rows.

      K 1 Reply Last reply
      0
      • B Bernhard Hiller

        Because user_access contains only one row matching useraccess_privileges. useraccess_privileges has 7 rows, user_access has 1 or more rows. Try a LEFT JOIN instead:

        SELECT useraccess_privileges.MenuName, user_access.*
        FROM useraccess_privileges
        LEFT JOIN user_access
        ON user_access.UserName=useraccess_privileges.UserName

        and you'll get again 7 rows.

        K Offline
        K Offline
        klaydze
        wrote on last edited by
        #3

        Hi Bernhard, Yep, i already did that but no luck. I don't understand why it return only 1 row. The work around that I did was like this.

        DataTable dt = new DataTable();
        MySqlCommand command = new MySqlCommand();
        command.CommandType = CommandType.Text;
        command.CommandText = "SELECT useraccess_privileges.MenuName, user_access.* FROM useraccess_privileges LEFT JOIN user_access ON user_access.UserName=useraccess_privileges.UserName";

        MySqlDataAdapter da = new MySqlDataAdapter(command);
        da.SelectCommand.Connection = conn;
        da.Fill(dt);

        If the above code was applied then it will return the correct row number. I really don't understand why. Thanks

        if(you type your code here) { Messagebox.Show("You help me a lot!"); } else { You help me = null; }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups