/    Sign up×
Community /Pin to ProfileBookmark

Joining tables question?

Hey, I’m retrieving data from a database after clicking a link from a previous page. Is there a way I can join my ‘articles’ with another table? I tried to put a left join after the FROM clause but I get an error.

At the moment I can only display data from the one table ?

[code=php]
if(!empty($_GET[‘id’]))
{
$sql = ”
SELECT *
FROM articles
WHERE art_book = “” .(int)$_GET[‘id’] . “”
ORDER BY id ASC
“;
[/code]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmApr 02.2017 — Is there a common piece of data between the two tables? That' show you do it.

<i>
</i>$q = "select a.fld1, a.fld2, a.fld3, b.fld1, b.fld2
from table1 a, table2 b
where a.fld1 = '$keyvalue' and
a.fld2 = b.fld1
order by a.fld2";


That is a simple join where only records that match a record in the other table will appear. If you need to get the complete set of records from a even if they don't have a match in table b, then you need to do an explicit 'join' query.

Tell us more and we can give you more, although this is a PHP forum not an SOL one. You might want to provide more data to that other forum rather than here.
Copy linkTweet thisAlerts:
@NogDogApr 02.2017 — For a left join, where you want all matches on your articles table and optionally any joins to the other table, it would be something like:
[code=php]
$sql = "
SELECT *
FROM articles
LEFT JOIN other_table ON articles.col_x = other_table.col_y
WHERE articles.art_book = "" .(int)$_GET['id'] . ""
ORDER BY id ASC
";
[/code]
×

Success!

Help @TheTrooper spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...