0

I have two tables.First table having two columns id and birds. Second table has two columns id and animals name. How to fetch this record using mysql and display it in a html single table in different rows.

Birds table

Animals table

output table

6
  • Have you made any attempts to solve this? If so please include your code Commented Apr 1, 2015 at 7:15
  • as the table field names are dfferent you need to use two different queries.... Commented Apr 1, 2015 at 7:16
  • sorry i have no idea on how to get this. so i didnt have any code. @Epodax Commented Apr 1, 2015 at 7:17
  • How we can use two different queries to display data in a same table.It would be helpful if i can get the code @Nishant Solanki Commented Apr 1, 2015 at 7:18
  • 1
    Something like this might work: SELECT a.id, a.Birds AS ResultData FROM tableA UNION SELECT b.id, b.Animals AS ResultData FROM tableB. Commented Apr 1, 2015 at 7:28

1 Answer 1

1

You can do as

select 
@r:=@r+1 as id , 
resultdata from 
(
  select birds as resultdata from birds 
  union all 
  select animals as resultdata from animals
)x,(select @r:=0)n;
Sign up to request clarification or add additional context in comments.

7 Comments

I just want to display the result data in a html table. No need to save it in database. @Abhik Chakraborty
This is not saving into the database, its selecting from the DB. You can run a loop and show the values into the html table.
Is it mysql query? I just want to display record from two tables in a single html table. I couldnt understant your code..can you please explain. @Abhik Chakraborty
Yes its mysql query. Its doing a union all from the two tables and then using a user defined variable @r to increment it for each record to provide an id as you have shown in the desired result. If you are using PHP then connect to DB using the php code, then execute the above query and then finally do a loop and display the data into the table.
Can you please share the code to use it in php.whether i need to use while loop to execute this query? @Abhik Chakraborty
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.