I have two tables which I need to select all rows from where the userid is $userid then sort them. I tried to use join but I couldn't even really get started on how to get it right. Can anybody point me in the right direction as to how to make these into one query?
$result1 = mysql_query("SELECT * FROM paypal_sub_info
WHERE userid='$THEuserid' ORDER BY cur_time DESC");
$result2 = mysql_query("SELECT * FROM paypal_pay_info
WHERE userid='$THEuserid' ORDER BY cur_time DESC");
while($row = mysql_fetch_array($result1)){
echo $row['txn_type'];
}
Solution:
SELECT *
FROM paypal_sub_info sub,paypal_pay_info pay
WHERE pay.userid = '$THEuserid'
AND sub.userid = '$THEuserid'
ORDER BY pay.cur_time DESC,sub.cur_time DESC
Try this:
SELECT * FROM paypal_sub_info sub, paypal_pay_info pay
WHERE pay.userid='$THEuserid' AND sub.userid='$THEuserid'
ORDER BY pay.cur_time DESC, sub.cur_time DESC
If you just want 'txn_type', you could make it a SELECT pay.txn_type AS txn_type
Use:
SELECT psi,*, ppi.*
FROM PAYPAL_SUB_INFO psi
JOIN PAYPAL_PAY_INFO ppi ON ppi.userid = psi.userid
WHERE psi.userid = $THEuserid
ORDER BY psi.cur_time DESC, ppi.cur_time DESC
I believe you want:
SELECT field1, field2, ... FROM paypal_sub_info WHERE userid='$THEuserid'
UNION
SELECT field1, field2, ... FROM paypal_pay_info WHERE userid='$THEuserid'
ORDER BY cur_time DESC
So first off consider using mysqli for for any serious project. OMG Ponies answer is how I would suggest doing it, thought you shouldn't have to specify the alias.wildcard fields separately in the select clause. It's also a best practice to actually specify the fields you are trying to fetch rather than *, though we all use * a lot when we're lazy.
Will A's answer makes me smile because it's technically what you asked for, though not what I expect you wanted.
Do you have a more detailed description of what data you're trying to extract, or was this just an example because you are having trouble figuring out joins?
-J
Related
Guys am trying to select the top/recently third row, i tried this one but it doesn't work, where do i make mistake ?
<?php
$sql = "SELECT * FROM songs ORDER BY id ASC LIMIT 1,2;";
$result = mysqli_query($con, $sql);
$resultCheck = mysqli_num_rows($result);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo $row['artist'];
}
}
?>
Use OFFSET:
SELECT * FROM songs ORDER BY id ASC LIMIT 1 OFFSET 2
The shorthand (which you are using) is reversed, so OFFSET is first then LIMIT:
SELECT * FROM songs ORDER BY id ASC LIMIT 2,1;
Use OFFSET
Here the limit 1 It simply means and you need one record
and the offset means skip the first 2
SELECT * FROM songs ORDER BY id ASC LIMIT 1 OFFSET 2
The parameters you use after limit should be reversed.
The first parameter is offset, and the second parameter is number of record you want.
SELECT * FROM songs ORDER BY id ASC LIMIT 2,1
This is just my opinion--
Sorting like this should always be done in client software.
Extract the data - remove the ORDER BY for your SQL...
Sort it in your client, and select and return the third line to the caller.
You will get better scalability and maintainability than driving all of this through an SQL query.
This is my go-to approach when solving these types of problems through custom software and it has been proven out over time.
Think about this:
Select ID from songs
get the id's into your code, and sort them there. Then chose the third one in the list. Then:
select title, author, artist, ... from songs where ID = VALUE FROM ID ABOVE
Yes, you are hitting the database twice, but these are two very efficient queries and that will perform better as your database scales, than the fancy order by you propose.
I want to output a nice user table. But my query needs a WHERE from multiple tables.
At the moment... my query looks like:
$statsTable = "someTable";
$userTable = "someOtherTable";
$someData = "SELECT stats.* FROM $statsTable stats, $userTable user
WHERE user.some_status = '0'
AND (stats.some_value BETWEEN $rangeFrom AND $rangeTo)
ORDER BY stats.some_value ASC
LIMIT 0,10";
then mysqli_query and so on...
The output(array) has 2 times the data from $statsTable and the WHEREs are not working. I just want to select the $statsTable...
How to proceed?
Thanks :)
$statsTable = "someTable";
$userTable = "someOtherTable";
$someQueryForData = "SELECT stats.*
FROM $statsTable stats
JOIN $userTable user
ON (user.id_stats = stats.id)
AND (user.some_status = '0')
WHERE (stats.some_value BETWEEN $rangeFrom AND $rangeTo)
ORDER BY stats.some_value ASC LIMIT 0,10";
Edit: explaining you're basically need a join, building query's the way you are doing makes them not as readable and you can't really associate your tables.
Using joins after you made your "ON" statement you may just add an "AND"
And use that conjunction as a where which is way faster the using the where ITSELF
Just use a join.
Join the tables on a unique ID and then you will have the values from both tables.
W3 Schools Joins
Should look like this
SELECT stats.* as stats, user.* as user
FROM statsTable
INNER JOIN userTable
ON stats.userId=user.userId
WHERE user.some_status = 0 AND (stats.some_value BETWEEN $rangeFrom AND $rangeTo)
LIMIT 0,10;
The following query take 10.86secs to initiate,
$sql="SELECT items.id i_id, status,manufacturerid,model,label,cpuno,corespercpu
from items,item2soft
where item2soft.itemid=items.id AND item2soft.softid={$r['id']}
order by label asc ";
While this code takes 23.73secs
$sql="SELECT items.id i_id, status,manufacturerid,model,label,cpuno,corespercpu
from items,item2soft
where item2soft.itemid=items.id AND item2soft.softid={$r['id']}";
The only difference between two codes is the latter has a ORDER BY keyword.Is there any way to make it faster.Please feel free to ask me anything.thanks for your help :)
After looking at your query - and by that I mean: adding proper indenting so I can actually read it - you probably just need to add some indexes.
$sql = "SELECT
items.id i_id,
status,
manufacturerid,
model,
label,
cpuno,
corespercpu
FROM
items,
item2soft
WHERE
item2soft.itemid = items.id
AND item2soft.softid = {$r['id']}
ORDER BY label ASC"
Add indexes on item2soft.itemid and item2soft.softid
If it's still slow, run an EXPLAIN
$active_sth = $dbh->prepare("SELECT * FROM user_campaign
WHERE status='blasting'
OR status='ready'
OR status='followup_hold'
OR status='initial_hold'
AND uid=:uid
ORDER BY status ASC");
$active_sth->bindParam(':uid', $_SESSION['uid']['id']);
$active_sth->execute();
I am positive $_SESSION['uid']['id'] = 7
but it will also pull results of id 10 or any other number.
Is my AND/OR clause written wrong?
Yes, query is wrong
SELECT * FROM user_campaign
WHERE (
status='blasting'
OR status='ready'
OR status='followup_hold'
OR status='initial_hold'
)
AND uid=:uid
ORDER BY status ASC
You have to group all ORs to make sure that row got one of this values, and separately check if it have given uid.
The proper way to write that is:
SELECT * FROM user_campaign
WHERE status IN ('blasting', 'ready', 'followup_hold', 'initial_hold')
AND uid =: uid
ORDER BY status ASC
You should use IN instead of that huge amount of ORs :)
i have following sql query, everything works fine but when i put "and posted_date<>$datetime" its not retrieving data as per given command.
$datetime="0000-00-00";
$data = mysql_query("SELECT * FROM product_table where category_id=$cat1 or
pid=$par or gpid=$gpar and posted_date<>$datetime
ORDER BY autoid desc limit $no2,$cacount")
or die(mysql_error());
please check is that line is ok maybe i am doing mistake somewhere where category_id=$cat1 or pid=$par or gpid=$gpar and posted_date<>$datetime
maybe i need two where one for or and another for and...
Thanks
Try to group your condition and use DATE()
SELECT *
FROM product_table
where (category_id=$cat1 or
pid=$par or gpid=$gpar) AND DATE(posted_date) <> DATE($datetime)
ORDER BY autoid desc
LIMIT $no2, $cacount