Query manipulation database - php

I am using some filters to display the products. Filters like colors, price and stuff.
Link : http://www.applechain.com/products/iPod.php
I use this query
$sql = "Select *
from tbl_product
where device='iPhone'
and (color='$c1'
or color='$c2'
or color='$c3'
or color='$c4'
or color='$c5'
or color='$c6'
or color='$c7'
or color='$c8'
or color='$c9'
or color='$c10'
) and (storage='$cp1'
or storage='$cp2'
or storage='$cp3'
or storage='$cp4'
or storage='$cp5'
) and (f_unlock='$factory')
and (warranty='$warranty')
and (price >= '$price1'
and price <= '$price2'
)
order by product_id desc";
Now the thing is that I want to show the result even if all the queries are not satisfied. How about if just couple of options are matched and i want to display results on basis of that.
I'll appreciate help.

$sql = "SELECT *
FROM tbl_product
WHERE device = 'iPhone'
AND color IN ('$c1', '$c2', '$c3', '$c4', '$c5',
'$c6', '$c7', '$c8', '$c9', '$c10')
AND storage IN ('$cp1', '$cp2', '$cp3', '$cp4', '$cp5')
AND f_unlock = '$factory'
AND warranty = '$warranty'
AND ((price >= '$price1') AND (price <= '$price2'))
order by product_id desc";

Try something like this query:
$sql = "
SELECT
*,
1 as order_prority
FROM tbl_product
WHERE
device = 'iPhone'
AND color IN ('$c1', '$c2', '$c3', '$c4', '$c5',
'$c6', '$c7', '$c8', '$c9', '$c10')
AND storage IN ('$cp1', '$cp2', '$cp3', '$cp4', '$cp5')
AND f_unlock = '$factory'
AND warranty = '$warranty'
AND (price BETWEEN '$price1' AND '$price2')
UNION SELECT
*,
2
FROM tbl_product
WHERE
device = 'iPhone'
AND (
color IN ('$c1', '$c2', '$c3', '$c4', '$c5',
'$c6', '$c7', '$c8', '$c9', '$c10')
OR storage IN ('$cp1', '$cp2', '$cp3', '$cp4', '$cp5')
OR f_unlock = '$factory'
OR warranty = '$warranty'
OR (price BETWEEN '$price1' AND '$price2')
)
ORDER BY order_prority ASC, product_id DESC
";
In all cases you will receive some result. If first SELECT (more strict) returns something, the data will contain those results in the top, and second SELECT (more soft) will be in the end. If first SELECT will return nothing, you will having the data from the second, soft, query.

Related

How to get data with order by id in MySQL

I want to show data from database if status is enable and package type is domestic and order by id desc. Here is my code:
$sql = "select *
from holidays
where pkg_status = 'Enable'
and pkg_type = 'Domestic'
limit 6";
I want to show this data as order by id desc. When I am trying to add query like this, it is not working.
$sql = "select *
from holidays
where pkg_status = 'Enable'
and pkg_type = 'Domestic'
limit 6
order by id desc";
Please help me.
limit is always written after group clause in SQL
Here you go, just copy and paste, you will be fine :
SELECT * FROM holidays
WHERE pkg_status='Enable'
AND pkg_type = 'Domestic'
ORDER BY id DESC LIMIT 6;

SQL: Get the most recent order from each customer

I am trying to display product from most recent orders on my main index page i have this code its working fine but i want to add order product thumb also in it which is stored in ecs_goods goods_thumb
function index_get_recent_buy_query()
{
$sql = 'SELECT ecs_users.user_name, ecs_order_goods.goods_id,
ecs_order_goods.goods_name, order_info.add_time, order_info.consignee
FROM ecs_users,
(
SELECT order_id, user_id, add_time, consignee
FROM ecs_order_info
ORDER BY ecs_order_info.add_time DESC
LIMIT 0 , 20
) AS order_info,
ecs_order_goods
WHERE order_info.order_id = ecs_order_goods.order_id
AND order_info.user_id = ecs_users.user_id;';
$all = $GLOBALS['db']->getAll($sql);
$arr[$idx]['goods_thumb'] = get_image_path($row['goods_id'],
$row['goods_thumb'], true);
I have the goods_id but i am confused on how can i include goods_thumb from ecs_goods in the above sql query
Here is the query part. I added a join with the ecs_goods table on the goods_id field and added goods_thumb field in the SELECT clause.
SELECT ecs_users.user_name, ecs_order_goods.goods_id,
ecs_order_goods.goods_name, order_info.add_time, order_info.consignee, ecs_goods.goods_thumb
FROM ecs_users,
(
SELECT order_id, user_id, add_time, consignee
FROM ecs_order_info
ORDER BY ecs_order_info.add_time DESC
LIMIT 0 , 20
) AS order_info,
ecs_order_goods, ecs_goods
WHERE order_info.order_id = ecs_order_goods.order_id
AND order_info.user_id = ecs_users.user_id
AND ecs_goods.goods_id = ecs_order_goods.goods_id;

MySQL 1 Table, 1 Select, Multiple Wheres

I am trying to add a "Related Products" section below my product. I can only get this to work if
I use two separate SELECTS. I am trying to combine them but can't get it to work.
Same Table in MySQL -
<?php
//select items from db
$items = mysql_query
("SELECT * FROM table WHERE (myinvno='dg300')
OR (action='alive' AND cate='dogs'
ORDER BY productNo ASC LIMIT 0, 4)");
or die(mysql_error());
while($item = mysql_fetch_array($items))
{
?>
This part is for the Related Products section:
OR (action='alive' AND cate='dogs'
ORDER BY productNo ASC LIMIT 0, 4)");
Thank you ~
The correct syntax would look like:
$items = mysql_query("
SELECT *
FROM table
WHERE (myinvno = 'dg300') OR
(action = 'alive' AND cate = 'dogs')
ORDER BY productNo ASC
LIMIT 0, 4"
);
You were missing a closing parent after 'dogs'.
EDIT:
Hmmm . . . I wonder if this is what you want:
SELECT *
FROM table
WHERE (myinvno = 'dg300')
UNION ALL
(SELECT *
FROM table
WHERE (myinvno <> 'dg300') AND
(action = 'alive' AND cate = 'dogs')
ORDER BY productNo ASC
LIMIT 0, 4
);
This gets all rows that match the first condition plus four more that match the second.

PHP select form a mysql table and order by another table

I have 2 tables in my MySQL Database:
customer
customer_billing
customer table has the columns columns
sequence
company
the *customer_billing* table has the columns
sequence
customer_seq
i am running a select query on the *customer_billing* table and then within a while loop a select query in the customer table like this:
$sql="SELECT *, SUM(quantity*unitprice) as customertotal from customer_billing where resellerid = '' and salesmanid = '' and producttype = '".$_GET["producttype"]."' group by customer_seq ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
while($result=mysql_fetch_array($rs))
{
$sql2="SELECT * from customer where sequence = '".$result["customer_seq"]."' and company_status = '' ";
$rs2=mysql_query($sql2,$conn) or die(mysql_error());
$result2=mysql_fetch_array($rs2);
}
i want to be able to order the results displayed by company ASC from the customer table
i have tried order_by customer_seq by obviously this only orders the sequence numbers
i have also tried doing order by company ASC on the customer table query but this didnt work either
how can i get round this?
You should make a join between customer and customer_billing on customer_seq and add company to the group by clause.
This way you should be able to order it by company.
You can use Joins and no need to use a second query try this,
$sql="SELECT c.*,ct.*, SUM(ct.quantity*ct.unitprice) as customertotal FROM
customer_billing ct , custom c WHERE ct.resellerid = '' AND ct.salesmanid = ''
AND c.company_status = '' AND c.sequence=ct.customer_seq AND
ct.producttype = '".$_GET["producttype"]."'
GROUP BY ct.customer_seq ORDER BY c.company ASC ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
while($result=mysql_fetch_array($rs))
{
// your code
}
You need to do this in a single query using join. Should be smth like this:
SELECT cb.customer_seq, c.sequence, c.company, SUM(quantity*unitprice) as customertotal
FROM customer_billing AS cb JOIN customer AS c
ON cb.sequence = c.sequence
WHERE cb.resellerid = ''
AND cb.salesmanid = ''
AND cb.producttype = '$_GET["producttype"]'
AND c.company_status = ''
GROUP BY cb.customer_seq, c.company
ORDER BY c.company

SQL query empty field, select ALL option like *

A php search page uses an SQL query to search for properties based on set criteria, in the example below the street "Harrison Road" is chosen:
search.php?city=1&rooms=3&rooms_upper=7&price_lower=55&price_upper=100&streetname=Harrison%20Road
I am looking for a way to leave the streetname blank and still SELECT ALL records as below:
search.php?city=1&rooms=3&rooms_upper=7&price_lower=55&price_upper=100&streetname=
I tried using the ' * ' symbol as with other SQL ocmmands to specify SELECT ALL but it does not work in this instance.
The problem is the search does not display any results while the street option is left blank.
The reason I am looking to run a search with an empty street criteria is because the search.php loads before the user selects a particular street using the drop down option on the page.
I would like it to SEARCH ALL records using the first criteria specified:
rooms, rooms_upper, price_lower, price_upper
The standard search page load has been left with the widest search criteria possible ( 3 < rooms < 7 ) and (£55 < rent < £75) in order to display ALL records before the user narrows the search criteria specifying a particular 'streetname' if desired.
Many Thanks!
Jeanclaude
The full SQL is here:
$sql=mysql_query("SELECT main_table.housenumber, main_table.housenumber, main_table.streetname,
max(main_table.rent) AS reviews_rent, main_table.rooms AS reviews_rooms,main_table.average,
houses.houseID, houses.path, houses.rooms AS houses_rooms,houses.rent AS houses_rent
FROM main_table
LEFT JOIN houses ON main_table.housenumber = houses.housenumber
AND main_table.streetname = houses.streetname
WHERE main_table.streetname='$page_streetname'
AND main_table.city=$city
AND main_table.verify='1'
AND main_table.rooms>='$rooms'
AND main_table.rooms<='$rooms_upper'
AND main_table.rent>=$price_lower
AND main_table.rent<=$price_upper
GROUP BY main_table.housenumber, main_table.streetname
ORDER BY average DESC, houseID DESC, reviewID DESC;");
I want to keep streetname in the WHERE clause, but I don't want to restrict the search if it is left blank.
How about creating the query dynamically.
Set up the base query without the streetname in the WHERE clause but include a sprintf string tag.
Then if $page_streetname has a value add the streetname selection to the query dynamically or if not just add nothing.
$q = "
SELECT main_table.housenumber,
main_table.streetname,
Max(main_table.rent) AS reviews_rent,
main_table.rooms AS reviews_rooms,
main_table.average,
houses.houseid,
houses.path,
houses.rooms AS houses_rooms,
houses.rent AS houses_rent
FROM main_table
LEFT JOIN houses
ON main_table.housenumber = houses.housenumber
AND main_table.streetname = houses.streetname
WHERE %s
main_table.city = $city
AND main_table.verify = '1'
AND main_table.rooms >= '$rooms'
AND main_table.rooms <= '$rooms_upper'
AND main_table.rent >=$ price_lower
AND main_table.rent <=$ price_upper
GROUP BY main_table.housenumber,
main_table.streetname
ORDER BY average DESC,
houseid DESC,
reviewid DESC";
$q = isset($page_streetname)
? sprintf( $q, "main_table.streetname = '$page_streetname' AND " )
: sprintf( $q, '');
That's easily solved with PHP (no need for sprintf()):
<?php
$page_streetname = empty($page_streetname)
? null
: "AND main_table.streetname = '{$page_streetname}'"
;
$result = mysql_query(
"SELECT
main_table.housenumber,
main_table.housenumber,
main_table.streetname,
max(main_table.rent) AS reviews_rent,
main_table.rooms AS reviews_rooms,
main_table.average,
houses.houseID,
houses.path,
houses.rooms AS houses_rooms,
houses.rent AS houses_rent
FROM main_table
LEFT JOIN houses
ON main_table.housenumber = houses.housenumber
AND main_table.streetname = houses.streetname
WHERE
main_table.city = '{$city}'
AND main_table.verify = 1
AND main_table.rooms >= '{$rooms}'
AND main_table.rooms <= '{$rooms_upper}'
AND main_table.rent >= {$price_lower}
AND main_table.rent <= {$price_upper}
{$page_streetname}
GROUP BY
main_table.housenumber,
main_table.streetname
ORDER BY
average DESC,
houseID DESC,
reviewID DESC"
);
?>

Categories