Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,156,039 members, 7,828,652 topics. Date: Wednesday, 15 May 2024 at 12:32 PM

Sql Query Help Needed - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / Sql Query Help Needed (768 Views)

How To Query The Interswitch Webservice / Help With SQL JOIN Query / Why Does This Mysql Query Display "array" Instead Of The Result (2) (3) (4)

(1) (Reply) (Go Down)

Sql Query Help Needed by webemerald(m): 6:52pm On Mar 28, 2008
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 dont seem to know how to do it.

Any info will be highly appreciated.
Re: Sql Query Help Needed by smartsoft(m): 7:27pm On Mar 28, 2008
SELECT * FROM books WHERE LEFT JOIN = id
Re: Sql Query Help Needed by my2cents(m): 8:18pm On Mar 28, 2008
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:



<?php
$result = mysql_query("SELECT * FROM books, authors WHERE books.author_id=authors.authors_id"wink;
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.
Re: Sql Query Help Needed by tundewoods(m): 10:06pm On Mar 28, 2008
smartsoft:

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'

(1) (Reply)

Have U All Seen The Reloaded Charlyboy's Site? Http://www.charlyboy.com What Do / How To Run Asp Codes In Windows Vista / Forum Set Up

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 12
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.