I am usually very good at retrieving data from a DB, but i have come to a stand still.
I have a search bar, which directs the user to another page to show the products they have searched for on the first page. My website allows users to search for takeaways in their area. On the second page i have a drop down list, this is used to search through the different food categories for that area.
My problem is i have almost 30 food categories, not all areas have takeaways joints for each of the 30 categories, i am trying to only show the food categories the area has and not show the others. My query so far shows all 30 categories, no matter the number of categories the area actually has, and when i set the WHERE to any other than restaurant_id the categories no longer show.
I have tried Inner joining 2/3 tables to make this query work, but no joy.
Delivery_Pcode Tbl
1 Del_code_id
2 Restaurant_ID
3 Pcode
4 Del_Price
Rest_Category tbl
1 CategoryID
2 Cuisine_category
3 Category_img
Rest_Details Tel
1 Resturant_ID
2 Resturant_name
3 City_name
4 Cat_ID
Query
$get_cats = "SELECT * FROM Rest_Category
INNER JOIN Rest_Details
ON Rest_Category.CategoryID = Rest_Details.Cat_ID
INNER JOIN Delivery_Pcode
ON Delivery_Pcode.Restaurant_ID = Rest_Details.Resturant_ID
WHERE Delivery_Pcode.Pcode= $searchq";
I am trying to select all from the tables where the Pcode is equal to the users input code. The postcodes each restaurant delivers to are saved in Delivery_Pcode.Pcode and $searchq is defined at the top of the page as the inputted postcode $_post value.
I have tried:
WHERE Restaurant_name = $rest_name (also defined)
WHERE Restaurant_name = $rest_name and Restaurant_id = $rest_id( also defined)
I don't know if i am thinking to deep into this, and there is a very simple method (i have a tendency to do that), or i need to delete the query and start again... and advise or guidance is much appreciated.
I know the query works, as it populates it is just the query which is causing me problems.
You can try adding quotes
"SELECT * FROM Rest_Category
INNER JOIN Rest_Details
ON Rest_Category.CategoryID = Rest_Details.Cat_ID
INNER JOIN Delivery_Pcode
ON Delivery_Pcode.Restaurant_ID = Rest_Details.Resturant_ID
WHERE Delivery_Pcode.Pcode= '$searchq'";
or (before a proper input sanitize)
"SELECT * FROM Rest_Category
INNER JOIN Rest_Details
ON Rest_Category.CategoryID = Rest_Details.Cat_ID
INNER JOIN Delivery_Pcode
ON Delivery_Pcode.Restaurant_ID = Rest_Details.Resturant_ID
WHERE Delivery_Pcode.Pcode= '" . $searchq ."';";
Related
I need help to this select where class from wordpress database:
What I would like to achieve is to display the first 6 newcomer posts/records.
The path to the image, image name, property title, property features (How many Bedrooms and Bathroom etc. there is)
property price and property price unit (if it $ or Danish kroner etc.)
At this link All properties here you can see what i will like to achieve on my front page, just without google map and the sort list and only the 6 first post:
All properties
This are tables: wp_wpl_properties, wp_wpl_units, wp_wpl_items to achieve my goal so i tried to make this select query:
enter code here
<?php
global $wpdb;
$results = $wpdb->get_results
("SELECT * FROM wp_wpl_properties,wp_wpl_units,wp_wpl_items
where wp_wpl_properties.price_unit= wp_wpl_units.id and wp_wpl_properties.id= wp_wpl_items.parent_id LIMIT 6;");
foreach ( $results as $result ) {
?>
I have attached table files to this question, here:
wp_wpl_units.pdf
wp_wpl_items.pdf
wp_wpl_properties (1).sql
The code I've made does not make any errors.
My problem is that i get the same record displayed 3 times at the fist columns and the same at the next 3 column, hope this make sens :)
Here is a link to my frontpage: My frontpage
Do you want like this?
$results = $wpdb->get_results ("
SELECT * FROM wp_wpl_properties
join wp_wpl_units on wp_wpl_properties.living_area_unit= wp_wpl_units.id
join wp_wpl_properties on wp_wpl_properties.id= wp_wpl_items.parent_id LIMIT 6");
or
$results = $wpdb->get_results ("
SELECT * FROM wp_wpl_properties
join wp_wpl_units on wp_wpl_properties.living_area_unit= wp_wpl_units.id
join wp_wpl_properties on wp_wpl_properties.id= wp_wpl_items.parent_id
where wp_wpl_properties.id= 1");
$results = $wpdb->get_results
("SELECT * FROM wp_wpl_properties
Inner Join wp_wpl_units ON wp_wpl_properties.living_area_unit = wp_wpl_units.id
Inner Join wp_wpl_items ON wp_wpl_properties.id= wp_wpl_items.parent_id
LIMIT 6;");
In your MySQL query,
SELECT * FROM wp_wpl_properties,
wp_wpl_units,
wp_wpl_items
where wp_wpl_properties.price_unit=wp_wpl_units.id
and wp_wpl_properties.id= wp_wpl_items.parent_id
LIMIT 6
MySQL individually checks each of the three columns differently and generates output for all of them even though they are same.
Doing an inner join instead of checking individually on three columns would be the solution. Like,
SELECT DISTINCT * FROM wp_wpl_properties as props
Inner Join wp_wpl_items as items ON items.parent_id = props.id
Inner Join wp_wpl_units as units ON units.id = props.living_area_unit
LIMIT 6
And instead of select distinct * from, get only the columns you need. it will speed up the query to a large extent!
like SELECT DISTINCT props.ID, items.ID FROM ....
Hope this helps.
I am trying to create a product search feature on an E-commerce website I am building but am having a little trouble.
I have 3 tables (categories, sub_categories, products)
Categories table fields: (categoryID, categoryName, active, image)
sub_categories table fields: (categoryID, categoryName, parentCatID, active, image)
products table fields: (productID, shortDescription, longDescription, image, catID, subCatID, active, price, delivery, weight)
Im trying to get my search to find a product if a user types in any part of the short description or long description or if the user types in any part of the category or sub category names it should find all products within those categories.
I dont know whether to do a JOIN or multiple SQL queries. to be honest i've been tinkering with it for a few hours but havnt really gotten anywhere and am now back at the drawing board asking for help
my first attempt looked like this:
$catSelect = mysqli_query($con,"SELECT * FROM categories WHERE categoryName LIKE '%{$term}%'");
$row1 = mysqli_fetch_row($catSelect);
$subCatSelect = mysqli_query($con,"SELECT * FROM sub_categories WHERE categoryName LIKE '%{$term}%' OR parentCatID = '%{$row1[0]}%'");
$row2 = mysqli_fetch_row($subcatSelect);
$productSelect = mysqli_query($con,"SELECT * FROM products WHERE short_description LIKE '%{$term}%' OR long_description LIKE '%{$term}%' OR subCatID = '%{$row2[0]}%' OR catID = '%{$row1[0]}%'");
my final attempt looks like this
mysqli_query($con,"SELECT * FROM products INNER JOIN categories ON products.catID = categories.categoryID WHERE categories.categoryName LIKE '%{$term}%'") or die(mysqli_error());
Could someone help me with the SQL query I need to use?
Try this:
SELECT
p.productID
FROM
products p
LEFT JOIN categories c
ON c.categoryID = p.catID
LEFT JOIN sub_categories sc
ON sc.categoryID = p.subCatID
WHERE
p.shortDescription LIKE '%keyword%'
OR p.longDescription LIKE '%keyword%'
OR c.categoryName LIKE '%keyword%'
OR sc.categoryName LIKE '%keyword%'
You need to do a join, on all three tables, with a where clause that queries the fields you want to search. Something like this:
select * from
products
inner join categories on products.catID = categories.categoryID
inner join sub_categories on products.subCatID = sub_categories.categoryID
where
products.shortDescription like '%query%'
or products.longDescription like '%query%'
or categories.categoryName like '%query%'
or sub_categories.categoryName like '%query%'
;
where the query is the search query string.
this can be helpful to you.....
SELECT * FROM products AS PT
LEFT JOIN Categories AS CT ON CT.catID = PT.catID
LEFT JOIN sub_categories AS SCT ON SCT.subCatID = PT.subCatID
WHERE
PT.active = 'YES' AND
(PT.shortDescription LIKE '%shortDescription%' OR
PT.longDescription LIKE '%longDescription%' OR
CT.category LIKE '%category%' OR
SCT.categoryID LIKE '%sub category%' )
you just put your values using php varrible you can get your search results.
Why don't you use lucene-solr for this?
Overview:
I want to get a list of profiles and the movies they like. All the output to be shown on one page. I am using mySQL and PHP, at moment running on localhost. Ideally i want this to be in one query, for speed.
Database table overview in mySQL as follow:
profile_tbl:
> id
> username
> about me
> gender
> hobbies
> profile_pic
> last_login
movies_tbl:
> id
> movie_name
> genre
> length
> rating
> description
profile_movies_rel_tbl
> id
> movie_id
> profile_id
Output:
I want to show 10 profiles and list of the all the movies they like. I was thinking following query:
SELECT profile_tbl.*, movies_tbl.* FROM profile_tbl
LEFT JOIN profile_movies_rel_tbl
ON profile_movies_rel_tbl.movie_id = movies_tbl.id
LEFT JOIN profile_tbl
ON profile_tbl.id= profile_movies_rel_tbl.profile_id LIMIT 10
I also have a second issue, where I want to lists all the profile that have selected movie has favorite, again the page should list profiles and movies together.
In this case, i was thinking of using the above query and adding following:
WHERE profile_movies_rel_tbl.movie_id = 4
Any one need more info, please leave comment.
thanks
I think you want to use LIMIT in a subquery to return 10 distinct profiles:
SELECT profile_tbl.*, movies_tbl.*
FROM (
SELECT DISTINCT Id FROM profile_tbl LIMIT 10
) LimitTable
INNER JOIN profile_tbl ON profile_tbl.Id=LimitTable.Id
LEFT JOIN profile_movies_rel_tbl
ON profile_movies_rel_tbl.profile_id = profile_tbl.id
LEFT JOIN movies_tbl
ON profile_movies_rel_tbl.movie_id = movies_tbl.id
This could return more than 10 rows, but it should only contain 10 profiles.
If you are wanting to return 10 rows, then look into using GROUP_CONCAT to combine your movies into a single column.
You can simple use joins for this. And using GROUP_CONCAT will
give you a list of all movies but comma seperated. Which you can explode
using php explode function whichi will give you the result in an array.
Than you can use loop that array for displaying movie names
SELECT
pt.*,
GROUP_CONCAT(movie_name) AS `Movies`
FROM profile_tbl AS pt
INNER JOIN profile_movies_rel_tbl AS pmrt ON pmrt.profile_id = pt.id
INNER JOIN movies_tbl AS mt ON mt.id = pmrt.movie_id
GROUP BY pt.id
LIMIT 10
ORDER BY pt.id DESC
so I'm having trouble selecting a field only if another fields value is not equal to 0.
So, here's what's going on.
I have 3 tables, they are - users, schools, campuses
And basically, I need to select a single users data from these 3 tables. I'd like to only select the campus_name field from campuses if the users.campus_id field is not 0.
So, something pseudo coded like this might give you a better idea..
The query is being passed in a $id variable, that has some user's id.
SELECT users.*, schools.*, (if(users.campus_id != 0) then campuses.campus_name)
FROM users, schools, campuses
WHERE users.id = '$id' (if(users.campus_id != 0) then AND campuses.id = users.campus_id)
SELECT *
FROM schools,
users LEFT OUTER JOIN campuses
ON users.campus_id != 0
AND users.campus_id = campuses.id
WHERE users.school_id = schools.id
So you are joining three tables together, you always expect a user to have a school, but they may not have a campus, and if this is the case, obviously nothing should be displayed.
The best you can do is acheive this with a LEFT JOIN on campuses.
An example based on your pseudo code below:
SELECT u.*, s.*, c.campus_name
FROM users u
INNER JOIN schools s ON u.school_id = s.school_id
LEFT JOIN campuses c ON u.campus_id = c.campus_id
WHERE u.user_id = $userId
If I understood you correctly, you want ALL the information displayed, but to display campus name ONLY if it exists? If so, I believe the following statement should do the trick:
SELECT `u`.* `s`.* if(`c`.`campus_name` != 0, `c`.`campus_name`, "") as `campus`
FROM `users` as `u`
INNER JOIN `schools` as `s` ON `u`.`school_id` = `s`.`school_id`
LEFT JOIN `campuses` as `c` ON `u`.`campus_id` = `c`.`campus_id`
WHERE `u`.`user_id` = $user_id;
Untested, written from the top of my head, could be errors in there
I am very new to SQL, so I am having a couple problems figuring things out. The database is for an online store, and the structure is something like this:
users: UserID, UserName, etc.
pricelists: PricelistID, UserID, ProductItemID, Price
productitems: ProductItemID, ProductID, ItemID
products: ProductID, ManufacturerID, WebPageText, etc.
Each product can have one or more items (e.g. if the product page is selling T-shirts, there may be 5 different T-shirt variations for sale). That's pretty normal. What's not normal is that the pricing for each item is determined by what's in the user's pricelist, because each user is assigned particular pricing. Not all users have a price assigned for every item.
Here is the question: The requirement from management is that the user should be able to see every product, even if that product is not in the user's pricelist. If the user has no pricelist entry for a productitem, the page should display, "Contact us for pricing." I have not been able to figure out how to do this with one query. Every join that I attempted involving the pricelist table threw out products for which the user didn't have a price assigned. I am given only the ProductID and UserID. I have put my code below, which is a mix of mysql and PHP. It works, but it is clunky, especially seeing as I have to redo the second query over and over. Please tell me how I really ought to be doing it.
$query_product = sprintf("SELECT * , (items.Stock - (SELECT Coalesce(Sum(DetailQuantity),0)
FROM orderdetails
INNER JOIN orders ON OrderID = DetailOrderID
INNER JOIN productitems ON orderdetails.ProductItemID = productitems.ProductItemID
INNER JOIN products ON productitems.ProductID = products.ProductID
INNER JOIN items ON productitems.ItemID = items.ItemID
WHERE OrderDate > ProductUpdateDate))*ProductLive
AS NumLeft
FROM productitems
INNER JOIN products ON products.ProductID = productitems.ProductID
JOIN items ON productitems.ItemID = items.ItemID
WHERE products.ProductID = %s",GetSQLValueString($ProductID, "int"));
$product = mysql_query($query_products, $x) or die(mysql_error());
$row_product = mysql_fetch_assoc($product);
//after narrowing down to one productitem, either through user selections or while looping through $row_product:
$query_price = sprintf("SELECT Price FROM pricelists
WHERE UserID = %s AND ProductItemID = %s", GetSQLValueString($UserID, "int"), GetSQLValueString($row_product['ProductItemID'], 'int'));
$price = mysql_query($query_price, $x) or die(mysql_error());
$row_price = mysql_fetch_assoc($price);
$row_product['Price'] = $row_price['Price'];
Later in the code, I check whether $row_products['Price'] is empty or not to determine what is displayed on the page.
EDIT: I thought this would be obvious, but ... I have to limit pricelists by WHERE UserID = %s. That means I can't just tack on a LEFT JOIN pricelists ON pricelists.ProductItemID = productitems.ProductItemID, which was actually the first thing I tried, and which does not work. Limiting the outer join in the WHERE statement turns it into an inner join.
Add the pricelists table as an LEFT join to your main query:
SELECT field1, field2, ..., pl.*
FROM orderdetails
INNER JOIN orders ON OrderID = DetailOrderID
...
LEFT JOIN pricelists pl PN pl.ProductItemID = productitems.ProductItemID
..
In your code check if the price in the pricelists table is null, if so, show the text.
You should look into using an outer join for this. Heres a great description of the difference between an inner and outer join -> inner join vs outer join