I'm trying to get a criteria from 2 Columns and indexing them using this query
$query1 = "SET #row_num = 0";
$query2 = "SELECT *, #row_num := #row_num + 1 as row_index FROM gift
WHERE Category = '0' AND ID ='".$ID."'
ORDER BY ID ASC;";
mysqli_query($conn, $query1);
$retrieve = mysqli_query($conn, $query2);
Is there is a way in which I can use the row_index as a variable in the query, like this:
$query1 = "SET #row_num = 0";
$query2 = "SELECT *, #row_num := #row_num + 1 as row_index FROM gift
WHERE Category = '0' AND row_index ='".$ID."'
ORDER BY ID ASC;";
mysqli_query($conn, $query1);
$retrieve = mysqli_query($conn, $query2);
You can test for the $ID in a HAVING clause:
$query1 = "SET #row_num = 0";
$query2 = "SELECT *, #row_num := #row_num + 1 as row_index FROM gift
WHERE Category = '0'
HAVING row_index = $ID
ORDER BY ID ASC;";
mysqli_query($conn, $query1);
$retrieve = mysqli_query($conn, $query2);
There is no need to concatenate the variable in the query. If $ID is an integer there is no need for quotes and if it is alpha-numeric just enclose it in single-quotes as PHP will interpolate the variable correctly.
Reference for HAVING clause
Without actually having tested it, I would expect something like the following to work....
SELECT ilv.*
FROM (
SELECT gift.*, #row_num := #row_num + 1 as row_index
FROM gift
WHERE Category = '0'
ORDER BY ID ASC
) ilv
WHERE row_index ='".$ID."';
(but you shouldn't be quoting an integer value).
Or....
SELECT *
FROM gift
WHERE Category = '0'
ORDER BY ID ASC
LIMIT $ID, 1;
Or, with a recent MySQL, using the MyISAM engine, you can define a primary key based on a natural key and autoincrement value. In your case, the "natural" key would be category. Then if you ensure that you are allocating the id stored in the database at a monotonic interval and simply.....
SELECT *
FROM gift
WHERE category='0'
AND id=($ID * $interval);
Related
<?php
$today=date('Y-m-d');
$sorgu = mysqli_query($conn, "SELECT * FROM urun");
while ($sira = mysqli_fetch_array($sorgu)) {
$urun = $sira['urun'];
$query = mysqli_query($conn, "SELECT product_name, birim, SUM(quantity) FROM
product WHERE product_name = '$urun' AND quantity != '0' AND quantity > '0'
AND grup!='uygulama' AND skt > '$today' GROUP BY product_name, birim ");
while ($row = mysqli_fetch_array($query)){
if ($row['2'] <= $sira['minstok']) {
echo count($row['0']);
} } } ?>
This code belongs to a table which has a notification system. I want the notification part increase when the line number increases in that table but it turns 11111111... , So how can I make the code give me sum?
Incidentally, everything up to while ($row... can be rewritten as follows:
SELECT p.product_name
, p.birim
, SUM(p.quantity) total
FROM urun u
JOIN product p
ON p.product_name = u.urun
AND p.quantity > 0
AND p.grup != 'uygulama'
AND p.skt > CURDATE()
GROUP
BY p.product_name
, p.birim;
I've got returns results from a query but I would like it to return multiple results, this is the current query:
$qry = "SELECT * FROM properties WHERE isLeased = 0 AND featured = 1 ORDER BY RAND() LIMIT 9";
$res = mysqli_query($mysqli, $qry) or die('-1' . mysqli_error());
What I would like is for it to return those results, as well as the results for:
$qry = "SELECT * FROM properties WHERE isLeased = 0 AND propertyType = 'For Sale' ORDER BY RAND() LIMIT 3";
$res = mysqli_query($mysqli, $qry) or die('-1' . mysqli_error());
But if I just add the second section of code below the first one it only returns the results of the first one.
How could I tell it to return results based on a couple of tables?
You can use a UNION query:
SELECT t1.*
FROM
(
SELECT *
FROM properties
WHERE isLeased = 0 AND featured = 1
ORDER BY RAND()
LIMIT 9
) t1
UNION ALL
SELECT t2.*
FROM
(
SELECT *
FROM properties
WHERE isLeased = 0 AND propertyType = 'For Sale'
ORDER BY RAND()
LIMIT 3
) t2
I have been try to combine two tables from mysql database, the two tables are status and status_reply both have the same columns number and name, that is id, account_name, author, postdate, data Please a help will be appreciated.
$limit = "LIMIT 0, 10";
$query = mysqli_query($db_conx, "(SELECT * `status` as type from status WHERE data LIKE '%".$tag."%' ORDER BY postdate DESC $limit)
UNION (SELECT * `status_reply` as type from status_reply WHERE data LIKE '%".$tag."%' ORDER BY postdate DESC $limit)");
//$query = mysqli_query($db_conx, "SELECT * FROM status WHERE data LIKE '%$tag%' ORDER BY postdate DESC $limit");
$statusnumrows = mysqli_num_rows($query);
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
$statusid = $row["id"];
$account_name = $row["account_name"];
$author = $row["author"];
$postdate = $row["postdate"];
$data = $row["data"];
$data = nl2br($data);
$data = str_replace("&","&",$data);
$data = stripslashes($data);
$statuslist .= '<div id="status_'.$statusid.'" class="status_boxes"><div><b>Ivotised by '.$author.' '.$postdate.':</b>
<article>'.$data.'</article>
</div></div>';
}
Use backquotes ` for field names instead of straight quotes '
Don't forget to quote $tag to protect from an SQL injection: mysqli_real_escape_string
Remember, that is you want to search literally for LIKE wildcard characters "%", "_" as well as backslash. You need to escape them too, using: $tag = addcslashes($tag, '\%_');
$limit = "LIMIT 0, 10";
$query = mysqli_query($db_conx, "
(SELECT `status` as type from status WHERE data LIKE '%".$tag."%'
ORDER BY postdate DESC $limit)
UNION
(SELECT `status_reply` as type from status_reply WHERE data LIKE '%".$tag."%'
ORDER BY postdate DESC $limit)");
I realized that I have to remove the type in both status and status_reply as references to the tables and identified each of the columns by their names. Am curious about it too!
$query = mysqli_query($db_conx, "
(SELECT id, account_name, author, postdate, data from status WHERE data LIKE '%".$tag."%'
ORDER BY postdate DESC $limit)
UNION
(SELECT id, account_name, author, postdate, data from status_reply WHERE data LIKE '%".$tag."%'
ORDER BY postdate DESC $limit)");
How can I implement something like this in mysql?
$query1 = "SELECT id FROM table WHERE username = 'John'";
$query2 = "SELECT id FROM table WHERE username= 'Parsa'";
$query = "SELECT * FROM table WHERE id BETWEEN $query1 AND $query2";
$result = mysql_query($query) or die('Query faild'.mysql_error());
$myrecord = mysql_fetch_assoc($result);
Try this
$query1 ="SELECT GROUP_CONCAT(id) FROM table WHERE firstname in('John','Parsa')";
$query = "SELECT * FROM table WHERE id IN ($query1)";
you have two identical queries , you could just have one . and use IN , not BETWEEN.
You can put those 3 queries in to one query:
$query = "SELECT * FROM table WHERE id
BETWEEN
( SELECT id FROM table WHERE firstname = 'John' GROUP BY id )
AND
( SELECT id FROM table WHERE firstname = 'Parsa' GROUP BY id )
";
although your query doesn't mean anything; you need "()" for subqueries to work.
$query1 = "(SELECT id FROM table WHERE username = 'John')";
$query2 = "(SELECT id FROM table WHERE username= 'Parsa')";
$query = "SELECT * FROM table WHERE id BETWEEN $query1 AND $query2";
u can use a subselection:
SELECT * FROM table WHERE id BETWEEN ($query1) AND ($query2)
But be careful: The Subselection result must be an Integer.
I was thinking of accomplishing the following as a PHP multi_query. But I'm trying to figure out how to pass the column value from the select query to the insert and update queries.
$query = "SELECT tbl_links.link, link_id
FROM tbl_links
INNER JOIN tbl_items ON tbl_links.item_id = tbl_items.item_id
WHERE tbl_items.item_name like '".$items_name[$counter]."'
AND NOT EXISTS (
select link_id
from tbl_clickedlinks
where tbl_clickedlinks.link_id = tbl_links.link_id
AND tbl_clickedlinks.cust_id = '$items_custID[$counter]'
)
limit 0, 1;" ;
$query .= "INSERT INTO tbl_claimedlinks (cust_id, link_id, claim_time) VALUES ('$items_custID', $row['link_id'], NOW()) ;";
$query .= "UPDATE tbl_links SET click_count = click_count+1 where link_id = '$linkID' ;";*/
Problem is, I'm not sure how to pass the link_id value to the other queries. So I'm thinking I might have to rearrange the queries into one, but again, I'm just not sure how to pull that off.
Anyone got any suggestions?
You need to execute select query 1st then use its output to execute 2nd & 3rd query.
$query = "SELECT tbl_links.link, link_id
FROM tbl_links
INNER JOIN tbl_items ON tbl_links.item_id = tbl_items.item_id
WHERE tbl_items.item_name like '".$items_name[$counter]."'
AND NOT EXISTS (
select link_id
from tbl_clickedlinks
where tbl_clickedlinks.link_id = tbl_links.link_id
AND tbl_clickedlinks.cust_id = '$items_custID[$counter]'
)
limit 0, 1;" ;
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$query2 = "INSERT INTO tbl_claimedlinks (cust_id, link_id, claim_time) VALUES ('$items_custID', $row['link_id'], NOW()) ;";
$query3 = "UPDATE tbl_links SET click_count = click_count+1 where link_id = '$linkID' ;";*/
mysql_query($query2);
mysql_query($query3);
}