0

I want to fetch value Rows from single table.I want to fetch sub_id for specific id.

I achieved my require ment in 2 query.I want to do it in single query.I want to display result as Event,order history,Eent Ticket,calander

[

$sql="select * from table1 where roles like %admin% and sub_id='0'"
$sql1=mysql_query($sql);
while($fet=mysql_fetch_assoc($sql1))
{
 $id=$fet['id'];
 $query="select page_name from table1 where sub_id= '$id'";
  .. ..
}
6
  • did you have problem with nested query? Commented Feb 4, 2016 at 6:29
  • $sql="select * from table1 where roles like %admin% and sub_id='0'" in this query you are actually getting page_name also . No need of second query in while loop Commented Feb 4, 2016 at 6:32
  • Why are you fetching page_name again as it is already retrieved in your first query? Commented Feb 4, 2016 at 6:32
  • if sub_id = 0 in ist than how can u get sub_id= '$id' ?? in second query? Commented Feb 4, 2016 at 6:33
  • @user3386779 why would you select the page_name again?You already have it in $fet variable. Commented Feb 4, 2016 at 6:44

5 Answers 5

4

Use a JOIN

SELECT t1.id, t1.sub_id, t1.page_name, t2.page_name AS parent_page
FROM table1 AS t1
JOIN table1 AS t2 ON t1.sub_id = t2.id
WHERE t2.roles like '%admin%' AND t2.sub_id = '0';

DEMO

Sign up to request clarification or add additional context in comments.

6 Comments

I want to display result as Event,order history,Eent Ticket,calander
That's what this does, see the demo I added.
I understand what you want. Please see my sqlfiddle demo, it returns the rows that you want.
If you want them all in a single row, with comma separators, use GROUP_CONCAT to combine them.
Now Its displays the rows where sub_id is != 0 now.
|
3

use this

  $sql="select sub_id from table1 where id='".$id."' "; 

After this, use results of this as below

  $sql= "select * from table1 where roles like %admin% and sub_id in($ids)";

Comments

1

You dont need another query to get the value of page_name just use $fet['page_name']; you already get the data of page_name in your first query.

$sub_id = $fet['sub_id'];//
echo $sub_id;//
$page_name = $fet['page_name'];//You can get and use the value of page_name here

UPDATED

if you want Event,Order History,Event Ticket and Calander

then change your where to sub_id = '2' ordered by id ascending.

$sql="select * from table1 where roles like %admin% and sub_id='2' order by id asc"
$sql1=mysql_query($sql);
while($fet=mysql_fetch_assoc($sql1))
{
  echo $fet['page_name'].'<br/>';//display the page_name
}

5 Comments

No, he wants a different row that's linked to the row he fetched in the first query, where sub_id = id.
@Barmar Why OP need to query again, when he only needs to get the value of page_name? he already have it in $fet variable.
$fet is the ID of the parent page in the hierarchy. He wants all the pages in the next level.
@Barmar no, $fet is an associative array with the index of id,sub_id,page_name and roles with its corresponding value base on OP's first query.. why use $fet['id'] just to select or get the value of sub_id?there is no need to get the value of something that you already have.Isn't it a redundancy?
$fet['page_name'] is comments and Event Management. He wants the child elements Events, Order History, etc.
1

You can also use the single query for getting page_name:

SELECT page_name FROM table1 
WHERE roles LIKE %admin% 
AND sub_id = 2

you can get Event,Order History,Event Ticket,Calendar as:

$sql="SELECT page_name FROM table1 
WHERE roles LIKE %admin% 
AND sub_id = 2";
$sql1=mysql_query($sql);

$records = array();
while($fet=mysql_fetch_assoc($sql1))
{
    $records[] = $fet['page_name'];
}

echo implode(",",$records); // Event,Order History,Event Ticket,Calendar

UPDATE 1:

use sub_id = 2 for getting all page_name related to Event MAnagement

Side note:

I suggest you to use mysqli_* or PDO, instead of mysql_* because mysql_* is deprecated in not available in PHP 7.

5 Comments

I need it in single query.
its single query brother @user3386779: SELECT page_name FROM table1 WHERE roles LIKE %admin% AND sub_id > 0
I need to start from 0. fetching subchild
than barmar query place in the same code. @user3386779
0
$sql="select sub_id from table1 where id='".$id."' "; 

if thats what you want.

Comments

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.