Page 1 of 1
mysqli multiple queries help needed :(
Posted: Sun Feb 20, 2011 7:24 pm
by Torniquet
Hey people.
Been looking around at mysqli's multiple query functionality, and struggling to find information i need.
I want to know how (if it is possable) to run a miltiple query using information grabbed from a query run in the same instance.
for example..
I want to run a query and get an ID from the query, then use the id from the 1st query to find information in another table. then use all the info to output.
is it possible? or would i have to run seperate queries?
thnx x much love
Re: mysqli multiple queries help needed :(
Posted: Sun Feb 20, 2011 7:42 pm
by Xaleph
You can use sub query`s.
SELECT something FROM some_table WHERE user_id IN (
SELECT user_id FROM some_other_table WHERE user_id = 10 )
AND item_level IN (
SELECT item_level FROM some_item_table WHERE hp >= 100 )
LIMIT 10
Re: mysqli multiple queries help needed :(
Posted: Sun Feb 20, 2011 7:45 pm
by Xaleph
You can also use VIEW tables. A "table" which is only a collection of data gathered from other tables, presented in a manner that pleases you. So every view is always a cumulative of other fields in existing tables. Only specifiying which ones you need for that view.
It`s easy to implement too, and less intensive since it`s going to be a schema in the DB, so it`s going to get cached by default.
Re: mysqli multiple queries help needed :(
Posted: Sun Feb 20, 2011 9:06 pm
by Torniquet
thats great. i will look into this shortly.
for now, i have another issue with a query. I am trying to order by before grouping. i have looked at aggregate querys.
but it dont seem to be working ¬¬
$sql = $DBA->query("
SELECT t1.*
FROM pbw_posts t1
LEFT JOIN pbw_posts t2 ON t1.topic_id = t2.topic_id AND t1.post_date < t2.post_date
WHERE t2.topic_id IS NULL
ORDER BY t1.post_date DESC
") or die(mysqli_error()); /// line 20
this is the error i am getting
Warning: mysqli_error() expects exactly 1 parameter, 0 given in Z:\blackwidow\widowweb\index.php on line 20
//// EDIT \\\\\
Ok never mind... turns out i was using post_date when it is named post_time (duh!)
and also found a different way (which seems ALOT simpler to understand)
$sql = $DBA->query("
SELECT *
FROM (
SELECT *
FROM pbw_posts
ORDER BY post_id DESC
) as tmp
GROUP BY
topic_id
ORDER BY
post_id DESC
LIMIT 5") or die (mysqli_error());