Jump to content

selecting data from a table based on data from another table


Lambneck

Recommended Posts

I get this error with the following code:

Error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY s.submission_id DESC LIMIT 25' at line 4 with query SELECT s.* FROM blog_tags s JOIN blog_tags a ON a.id = s.blogging WHERE a.id = ORDER BY s.submission_id DESC LIMIT 25

 

	$id = $_GET['id']; 
$idFromOtherTable = $row['post_id'];
$sql = "SELECT s.* FROM $table s
        JOIN $table1 a ON a.id = s.$id  
        WHERE a.id = $idFromOtherTable  
        ORDER BY s.submission_id DESC  
        LIMIT 25";

Link to comment
Share on other sites

its sup[posed to be

 

	

$id = $_GET['id']; 



$idFromOtherTable = $row['post_id'];
$sql = "SELECT s.* FROM $table as s
        JOIN $table1 as a ON a.id = s.$id  
        WHERE a.id = $idFromOtherTable  
        ORDER BY s.submission_id DESC  
        LIMIT 25";

Link to comment
Share on other sites

In this line, do not use the $id variable in it.

 

JOIN $table1 a ON a.id = s.$id // incorrect

JOIN $table1 a ON a.id = s.id // correct

This line just joins the tables together based on the id fields. If you have to get the results for two different id's you will do it in WHERE part.

Link to comment
Share on other sites

Could you please explain what are the two different id's? $_GET['id'] and $row['post_id'] and how they should be used? Maybe also provide the information how you have created your table structures. I don't understand why you have two different id's.

Link to comment
Share on other sites

So you want to get all the submissions related to one post_id right? If so.. (did not test it but should work)

 

<?php
$id = $_GET['id']; // This is the id post_id

// Get submissions related to post_id
$sql = "SELECT s.* FROM post_table p
	  JOIN submissions_table s ON s.submission_id = p.post_id  
	  WHERE p.post_id = $id";
$result = mysql_query($sql);

Link to comment
Share on other sites

Yeah  thats what I'm trying to do. Only the $_GET['id'] value isnt 'post_id' its the value of the row 'tag'. So the value of the row 'tag' is passed in the url gotten by $_GET['id']. Then this row identified by $_GET['id'] or '$id' has a column called 'post_id' in it which contains the id or the 'submission_id' of the information in the table containing the display data.

 

So the tables are connected via the post_id / submission_id number.

But the row initiating the connection is defined by the value passed in the url (which is contained in the 'tag' column)

 

-(I guess if I knew how to explain this better I wouldnt need the help. Sorry for the confusion)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.