Sql Query Help Needed

A Member? Please Login  
type your username and password to login
Date: December 05, 2008, 12:23 AM
268300 members and 165317 Topics
Latest Member: Vincent 00
Nairaland [Nigerian Forum] Home Help Search Who is currently online? Login Register
Nairaland Forum  |  Technology  |  Webmasters (Moderator: OmniPotens)  |  Sql Query Help Needed
Pages: (1) Go Down Send this topic Notify of replies
Author Topic: Sql Query Help Needed  (Read 103 views)
webemerald (m)
Sql Query Help Needed
« on: March 28, 2008, 06:52 PM »

hi folks, i need an sql query(mysql) that can retrieve records from two tables and which also  can reterive specific records e.g

lets say i have to tables, books and authors and both referenced by a foreign key(author_id)  in the books table.

if i use  a join command eg SELECT * FROM books LEFT JOIN authors ON books.author_id=authors.authors_id;

i know this will retreive all records but what if i want only only one record or just a select group of records, normally i should use my WHERE but i don't seem to know how to do it.

Any info will be highly appreciated.
smartsoft (m)
Re: Sql Query Help Needed
« #1 on: March 28, 2008, 07:27 PM »

SELECT * FROM  books  WHERE  LEFT JOIN = id
my2cents (m)
Re: Sql Query Help Needed
« #2 on: March 28, 2008, 08:18 PM »

Off the top of my head, I would imagine that your result is stored in an array (I am thinking one of the mysql_fetch_* functions here) and based on which result you want you can then reference the index/indices of the rows you need.

I have never seen a retrieval written your way (I am not saying it is wrong. I am just saying I have not seen it written your way) so please allow me to write it the way I probably would:


Code:
<?php
$result
= mysql_query("SELECT * FROM books, authors WHERE books.author_id=authors.authors_id");
if (!
$result) {
    echo
'Could not execute! ';
    exit;
}
$row = mysql_fetch_row($result);

echo
$row[0]; //the first row of the result, e.g. email
echo $row[2]; //the second row of the result, e.g. name
echo $row[6];
echo
$row[n]; //to nth row of result
?>


Then to get multiple records, you do the same thing, but in a loop (this may be where mysql_fetch_array comes in).  One thing I like to do is insert an "echo" after each line I want executed and then add an "exit" after each step (declare query, exit.  get results, exit. etc).

Again, this is off the top of my head.  I haven't ran this code so there is no guarantee that this will work, but hopefully you get my point.
I hope this helps.
tundewoods (m)
Re: Sql Query Help Needed
« #3 on: March 28, 2008, 10:06 PM »

Quote from: smartsoft on March 28, 2008, 07:27 PM
SELECT * FROM books WHERE LEFT JOIN = id

Thank you very much for pointing out that it sql retrieval queries such as the above are out right un retrievable,however to tackle to point raised.i guess what is being missed out is that when writing an sql query to retrieve data from 2 or more tables of a database,be it using the INNER JOIN or LEFT JOIN,the fundamental points to be considered is to ensure that both tables that the data is to retrieved from is specified as well as the foreign key value such as ( WHERE books.author_id=authors.authors_id) that provides balance in the cantilever.

However you will need one or two filter based fields that will be useful in your specific record filter which are usually ENUM fields that have either 'Yes' or 'No'
 Can U Handle This?  Do We Pay Tax For Online Income?  Turnkey Adsense Ready Website  Page 2
Pages: (1) Go Up Send Topic to Friend by E-mail Reply 
Google
 
Web www.nairaland.com
Sections: TV/Movies (2) Music/Radio (2) Celebrities Job Talk Jobs/Vacancies (2) Career Talk Romance Books Politics Sports Fashion Travel
Health Schooling Religion General(2) Business Webmaster Programming Computers Phones Cars & Trucks

Links: Page1 Page2 Page3 Page4 Page5 Page6 Page7 Page8 Page9 Page10

Nairaland is owned by Oluwaseun Osewa
Nairaland Forum | Powered by SMF 1.0.12.
© 2001-2005, Lewis Media. All Rights Reserved.