Schema: http://pastebin.com/jU3HmmNM
$mainfetch = "SELECT * FROM status ORDER BY status_id DESC LIMIT 10,0";
while($mainfetch -> $result) {
$subquery = SELECT COUNT(*) FROM status s, likes l WHERE s.status_id = l.status_id AND l.member_id = 1;
$liked = $query->row();
if($liked > 0) {
//liked
}else {
//not liked
}
}
So this is not optimal as $subquery will execute each time 10 times, I wish to add $subquery to $mainfetch, how can I join them two?
You just need to add a left join on s.status_id = l.status_id... And then you get also the l.status_id: if it is null, then member 1 not liked it, otherwise yes of course.
Using the schema you provided:
SELECT s.status_id, s.text_id, count(l.status_id) as like_count
FROM status AS s LEFT JOIN likes AS l ON (s.status_id = l.status_id AND l.member_id = 1)
group by s.status_id, s.text_id
ORDER BY status_id DESC
LIMIT 10,0
Related
I am tracking when sellers sell below a certain price on a product on Amazon. I am storing products, sellers, and violations in the database. I am attempting to display the number of sellers who are violating within a 24 hour period. Rather than displaying the number of sellers violating, I am receiving the total number of violations.
ViolationsRepository.php
public function countNumberOfSellersInViolationsForVendorInLast24Hours($vendorId)
{
$select = "SELECT DISTINCT COUNT(
CASE
WHEN v.source = 'amazon' THEN
(SELECT DISTINCT s.id
FROM seller_info_amazon AS sai
LEFT JOIN sellers_amazon AS sa ON sa.amazon_id = sai.id
LEFT JOIN sellers AS s ON sa.seller_id = s.id
WHERE sai.unique_id = v.seller_id_amazon AND s.id IS NOT NULL
LIMIT 1)
WHEN v.source = 'ebay' THEN
(SELECT s.id
FROM sellers AS s
WHERE s.id = v.seller_id_ebay
LIMIT 1)
WHEN v.source = 'google' THEN
(SELECT s.id
FROM sellers AS s
WHERE s.id = v.seller_id_google
LIMIT 1)
END)
FROM
violations AS v
";
$timeLimit = new \DateTime('24 hours ago');
$where = " WHERE v.vendor_id = :vendorId AND v.last_scout_date >= :timeLimit \n";
$query = $this->getEntityManager()->getConnection()->prepare($select . $where );
$query->bindValue(':vendorId', $vendorId);
$query->bindValue(':timeLimit', $timeLimit->format("Y-m-d h:i:s"));
$query->execute();
return $query->fetchColumn();
}
sellers_amazon TABLE
seller_id amazon_id
violations TABLE
id vendor_id seller_id_amazon last_scout_date source
seller_info_amazon TABLE
id unique_id name
sellers TABLE
id originating_vendor_id name
Solved this - SELECT COUNT(DISTINCT ... instead of SELECT DISTINCT COUNT( ....
i need select some data from two tables ,
please help me use inner join for this selection .
players in selction2 must not be in selection1...
first select :
$rs = "SELECT *
FROM `player`
WHERE `status`=1 AND `credit`>=1 AND `username` NOT LIKE '$user'
ORDER BY ls ASC,credit DESC
LIMIT 0 ,10;
Second: this players must remove from result of selection1
$rs2 = "SELECT *
FROM `ip_log`
WHERE `playerid`='$ui' AND `win`='1' AND `date`='$date' ";`
You can use LEFT JOIN for this:
This shows the log messages for everyone not in selection 1.
SELECT l.*
FROM ip_log AS l
LEFT JOIN
(SELECT username
FROM player
WHERE status = 1 AND credit >= 1 AND username NOT LIKE '$user'
ORDER BY ls ASC, credit DESC
LIMIT 10) AS p
ON l.player = p.username
WHERE win = 1 and date = '$date'
AND p.username IS NULL
This shows the top 10 player data, except the ones with log messages in selection 2
SELECT p.*
FROM player AS p
LEFT JOIN ip_log AS l ON l.player = p.username AND l.win = 1 AND l.date = '$date'
WHERE p.status = 1 AND p.credit >= 1 AND p.username NOT LIKE '$user'
AND l.player IS NULL
ORDER BY p.ls ASC, p.credit DESC
LIMIT 10
In both cases, testing a column in the second table with IS NULL makes it return only the rows in the first table that don't have a match in the second table. See
Return row only if value doesn't exist
You can do it with LEFT JOIN
SELECT player.*,ip_log.* FROM `player` LEFT JOIN `ip_log` ON player.id!=ip_log.playerid GROUP BY player.id
I have a 4 table merge going on and in the end I want it to be sorted by an "ORDER BY" according to some variable. Right now this is always returning the same order of entries.
Something like:
if(isset($_GET['filter'])){
$filter = $_GET['filter'];
#Example $filter = 'date' or team or game_num
$q = $db->prepare("SELECT g.game_num, s.date, t.team
FROM schedule n
LEFT JOIN g_lkp g
ON n.game_num = g.game_num
LEFT JOIN dates s
ON n.date = s.date
LEFT JOIN teams t
ON n.home_team_nbr = t.team
ORDER BY '$filter' ");
$q->execute();
$qR = $q->fetchAll(PDO::FETCH_ASSOC);
if ($q->rowCount() > 0) {
foreach ($qR as $row) {
echo '
//First check value of $filter
$q = $db->prepare("SELECT g.game_num game_num, s.date date, t.team team
FROM schedule n
LEFT JOIN g_lkp g
ON n.game_num = g.game_num
LEFT JOIN dates s
ON n.date = s.date
LEFT JOIN teams t
ON n.home_team_nbr = t.team
ORDER BY $filter ");
I have a working PHP script that selects the content with a join of a user table. Then I while loop the results.
However, I was wondering if it would be possible to instead of calling another query each loop, call it in the initial query? Basically merging them?
Code:
$content_result = mysql_query("
SELECT content.id, content.type, content.title, content.url, users.username
FROM content
INNER JOIN users ON content.uploaderuid = users.id
ORDER BY content.id DESC
LIMIT $offset, $rowsperpage
");
while($content_row = mysql_fetch_array($content_result)){
$contentid = $content_row['id'];
$result_num_votes = mysql_fetch_assoc(mysql_query("SELECT COUNT(*) FROM votes WHERE votedcontentid = '$contentid' AND value = '1'"));
$num_votes = $result_num_votes['COUNT(*)'];
}
Try this:
SELECT COUNT(votedcontentid)as numbervotes,content.id, content.type, content.title, content.url, users.username
FROM content
INNER JOIN users ON content.uploaderuid = users.id
JOIN votes ON votes.votedcontentid = content.id
ORDER BY content.id DESC LIMIT $offset, $rowsperpage
And here change it to
$num_votes = $result_num_votes['numbervotes'];
So i'm filling in for our developer at the moment (be for-warned i'm a beginner) but I"m trying to simply sort my search results by profiles that have a profile picture included (i.e, i don't want blank profile pictures to show up at the top of the results...they should all be at the end of the results)...Note that there are a couple user types which is why there is so much code...
I'm pretty sure where i'm going wrong is the 2 lines...
ORDER BY $order u.picture ISNULL DESC"; (which relates to ordering by profile pictures). Would really appreciate any and all help...thx!
The code is as follows:
if ($user_type == 1) {
$sql = "SELECT a.*, u.*,
(SELECT COUNT(DISTINCT userId) FROM LF_usertype_A WHERE usertype_BId = u.userId AND status = 1) as i_cnt,
(SELECT COUNT(productId) FROM LF_products WHERE userId = u.userId AND status = 1) as product_cnt,
(SELECT COUNT(transactionId)
FROM LF_Transactions
WHERE usertypeBId = u.userId
AND (status = 1 OR status = 2)
AND type = 9
AND userId != usertypeBId
AND userId != usertypeAId) AS cnt
FROM LF_Users u
JOIN LF_products a ON a.userId = u.userId
LEFT JOIN LF_Transactions t ON t.productId = a.productId
WHERE a.status = 1
AND u.status = 1
AND u.userType = :ut $where
GROUP BY u.userID
ORDER BY $order u.name DESC LIMIT 200";
} elseif ($filter != "recent" && $user_type == 2) {
$sql = "SELECT u.*,
(SELECT COUNT(a.productId) FROM LF_usertypeA a INNER JOIN LF_products ON a.productId = m.productId INNER JOIN LF_Users uu ON uu.userId = a.usertypeAId WHERE a.userId = u.userId AND uu.status = 1 AND a.status = 1 AND m.status = 1) as product_cnt,
(SELECT COUNT(transactionId)
FROM LF_Transactions
WHERE usertypeBId = u.userId
AND (status = 1 OR status = 2)
AND type = 9
AND userId != usertypeAId
AND userId != usertypeBId) AS cnt
FROM LF_Users u
LEFT JOIN LF_Transactions t ON t.usertypeBId = u.userId
WHERE u.status = 1
AND u.userId != 1
AND u.userType = :ut $where
GROUP BY u.userID
ORDER BY $order u.name DESC LIMIT 200
ORDER BY $order u.picture ISNULL DESC";
} else {
$sql = "SELECT u.*,
(SELECT COUNT(a.productId) FROM LF_usertype_A a INNER JOIN LF_products m ON a.productId = m.productId INNER JOIN LF_Users uu ON uu.userId = a.usertypeAId WHERE a.userId = u.userId AND uu.status = 1 AND a.status = 1 AND m.status = 1) as product_cnt,
(SELECT COUNT(transactionId)
FROM LF_Transactions
WHERE usertypeBId = u.userId
AND (status = 1 OR status = 2)
AND type = 9
AND userId != usertypeAId
AND userId != usertypeBId) AS cnt
FROM LF_Users u
WHERE u.status = 1
AND u.userId != 1
AND u.userType = :ut $where
GROUP BY u.userID
ORDER BY $order u.name DESC LIMIT 200
ORDER BY $order u.picture ISNULL DESC";
}
You would have to put the isnull condition before your regular sort order if you want it to take precedence:
ORDER BY ISNULL(u.picture), $order