PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, February 20, 2022

[FIXED] In join query to table field have same name means how get the two fields value?

 February 20, 2022     mysql, php, phpmyadmin     No comments   

Issue

I used a join query to retrieve values from two tables that have the same field name. How can i get the two fields value?

mysql_query("SELECT table1.Name, table2.Name 
FROM table1
INNER JOIN table2
ON table1.Id=table2.userid
ORDER BY table1.Id DECS LIMIT 5") 

With the above query, I need values from both table1.Name and table2.Name.


Solution

Give alias,

SELECT table1.Name as table1Name, table2.Name as table2Name
FROM table1
INNER JOIN table2
ON table1.Id=table2.userid
ORDER BY table1.Id DECS LIMIT 5

Note: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.



Answered By - Rikesh
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing