I want to implement a search function in my application based on these tables.
I have 5 fields on search page
Input box - Keywords or any word in the title
Author - Select Box (author_id will be passed to the function as value)
Category Name - Select Box (category_id will be passed to the function as value)
themonth - Select Box from 1 - 12
theyear - Select Box from 2000 - 2012
I want to create a search query from based upon these rules,
Results array will be sorted by insights.read_time, (how many times the article has been read)
Only want to get the article.article_id
Pre-mature working working example is here
I am running following query to get but it is not complete
https://leading-people.com/search
SELECT article.article_id FROM article WHERE
article.is_active = '1' AND (content LIKE '%%' OR title LIKE
'%%' OR tags LIKE '%%') UNION ALL SELECT article.article_id
FROM keywords INNER JOIN article WHERE article.is_active = '1'
AND (article.article_id = keywords.article_id AND
keywords.keywordtext LIKE '%%')
TABLE article
COLUMNS
article_id (PRIMARY)
is_active
title
themonth
theyear
TABLE article_author
Comments: This table is just for reference author details is in another table. So these id(s) are just for reference.
COLUMNS
article_author_id (PRIMARY)
author_id
article_id
TABLE article_categories
Comments: This table is just for reference categories details is in another table. So these id(s) are just for reference.
COLUMNS
article_categories_id (PRIMARY)
article_id
categories_id
TABLE insights
Comments: This table is just for reference categories details is in another table. So these id(s) are just for reference.
COLUMNS
insights_id (PRIMARY)
article_id
read_time
TABLE keyword
Comments: This table is just for reference categories details is in another table. So these id(s) are just for reference.
COLUMNS
keyword_id (PRIMARY)
article_id
keywordtext
Hope! I've formatted it correctly so everyone can understand!
This might help you in your quest:
$keyword_mysql= mysql_real_escape_string($keyword);
SELECT article.article_id
FROM article
LEFT JOIN insights ON(article.article_id=insights.article_id)
WHERE article.is_active = '1' AND (content LIKE '%{$keyword_mysql}%' OR title LIKE '%{$keyword_mysql}%' OR tags LIKE '%{$keyword_mysql}%')
UNION DISTINCT
SELECT article.article_id
FROM keywords INNER JOIN article WHERE article.is_active = '1' AND (article.article_id = keywords.article_id AND keywords.keywordtext LIKE '%{$keyword_mysql}%')
LEFT JOIN insights ON(article.article_id=insights.article_id)
ORDER BY insights.read_time
You also could escape underscores and percents in $keyword_mysql with a backslash.
I'm not sure whether the ordering will work if the read_time column is not part of the selected fields. If not, add it to the selected fields and ORDER BY read_time instead.
After struggling for 6,7 hours, I was amazed that it was so simple :-(
Here is the solution that I came up with
SELECT article.article_id FROM article WHERE $cond (content LIKE '%$term%' OR title LIKE '%$term%' OR tags LIKE '%$term%')
AND article.article_id IN (SELECT article_categories.article_id FROM article_categories WHERE categories_id LIKE '$cat') AND article.article_id IN (SELECT article_author.article_id FROM article_author WHERE author_id LIKE '$author')
UNION ALL SELECT article.article_id FROM keywords INNER JOIN
article WHERE $cond (article.article_id =
keywords.article_id AND keywords.keyword LIKE '%$term%') AND
article.article_id IN (SELECT article_categories.article_id
FROM article_categories WHERE categories_id LIKE '$cat') AND
article.article_id IN (SELECT article_author.article_id FROM
article_author WHERE author_id LIKE '$author')
Related
I am trying to make an sql search query to return only ORDERS that match my search keyword. The issue is that i also need to search in every comments body for each order and if a result is found in that comment the order for that comment should be selected. The tables looks like this in a simplified way:
ORDERS:
ID = 95
title = first order
COMMENT RELATIONS:
id = 1241
comment_id = 500
target_id = 95
type = order
COMMENTS:
id = 500
body = this is the first comment
So if i searched for "first comment" the order with id 95 should be selected.
My current attempt seems to select more then order but also their comments and relations? This is how my current attempt looks like.
$query = 'SELECT DISTINCT *
FROM orders as o
LEFT JOIN comment_relations as cr ON o.id = target_id
LEFT JOIN comments as c ON cr.comment_id = c.id
WHERE o.id LIKE :keyword OR o.title LIKE :keyword OR c.body LIKE :keyword';
Not sure which database you are using but in most databases you can use the table alias follow by * for selecting only one table.
'SELECT DISTINCT o.*
FROM orders as o
LEFT OUTER JOIN comment_relations as cr ON o.id = target_id
LEFT OUTER JOIN comments as c ON cr.comment_id = c.id
WHERE o.id LIKE :keyword OR o.title LIKE :keyword OR c.body LIKE :keyword';
As per your concept i create 3 demo tables in phpmyadmin which description is here.
1.orders have columns (id,title).
2.comment_relations have columns (id,comment_id,target_id,type).
3.comments have columns (id,body).
This is SQL Query for fetch records from order by match keyword from comments table.
select DISTINCT o.* from orders o LEFT JOIN comment_relations cr ON o.id=cr.target_id LEFT JOIN comments c ON c.id=cr.comment_id where body LIKE '%keyword%'
On applying this query by replace your keyword at place of keyword you get this result.
Here i match keyword "first comment"
I have 3 tables in my database, Categories,Subcategories and Articles.
My Articles table contains columns caled CategoryID and SubcategoryID, which are foreign keys of the table Categories and Subcategories.
My question is, how can I get the name of the Category and Subcategory stored only as ID's in my Articles Table.
I think I need a kind of subquery or join.
Here is what I have:
SELECT ArticleTitle
,CategoryID
,SubcategoryID
FROM Articles
WHERE SubcategoryID =
(SELECT SubcategoryName
FROM Subcategories
WHERE SubcategoryName = 'info'
)
When I execute this code in mysql, there are no erros, but I receive 0 results. All tables are containing some data.
change this:
where SubcategoryID = (select SubcategoryName from Subcategories
to this
where SubcategoryID in (select SubcategoryID from Subcategories
the changes were
the equal sign is now the word in.
the subquery is selecting SubcategoryID instead of SubCategoryName
Using Joins :
SELECT a.ArticleTitle AS ArticleTitle,
c.CategoryName AS CategoryName,s.SubcategoryName AS SubcategoryName
FROM
Articles a JOIN Categories c on a.CategoryID=c.ID JOIN Subcategories s on a.SubcategoryID=s.ID
WHERE s.SubcategoryName = 'info';
I'm trying to join 2 tables with same categories but it's not quite working, its not returning results.
I have 2 tables tvshows and movies and both of them have columns categories now i want to go through all of them and see if categories match and return them but I don't know what is wrong with this. Please take a look, thanks!
$query = $connection->prepare("
SELECT
a.id,
a.hash,
a.categories,
a.thumb_location,
a.name,
a.year,
b.id,
b.hash,
b.categories,
b.thumb_location,
b.name,
b.year
FROM movies AS a
LEFT JOIN series AS b
ON a.categories = b.categories
WHERE a.categories LIKE ? OR b.categories LIKE ? ORDER BY a.id DESC
");
You are using column named categories in WHERE part of your query, but there are two columns with this name in tables movies and series, so database won't know, which column to search in.
You have to specify probably WHERE a.categories LIKE ? OR b.categories LIKE ? if you want to find rows, where some category is set for movie or series.
If you want to search for movies and series pairs by categories, you should join them by that column. But if categories is something like "Comedy,Action" etc. it won't work. You should consider remodeling database and create table categories and then movies_categories with ID of movie and ID of category rows and series_categories with same structure. Than you will be able to do lot's of queries for selecting movies that has categories same as series etc.
I'm not sure I've understood very well, but give this a try:
$query = $connection->prepare("
SELECT a.hash,
a.categories,
a.thumb_location,
a.name,
a.year,
b.hash,
b.categories,
b.thumb_location,
b.name,
b.year
FROM movies AS a
LEFT JOIN series AS b
ON a.hash = b.hash
WHERE a.categories = b.categories ORDER BY id DESC
");
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?
I am trying to select from 2 databases on the same server that have the exact same structure and db2 is a continuance of db1. The problem is I am trying to select the same info "different unique id's though" from both databases and I need the query formatted as such: link_id AS id, title AS title to present the info correctly using $row['title'] etc. Here is my query:
$CONF['mysql_query'] = 'SELECT db1.l.link_id AS id, db1.l.title AS title, db2.r.link_id AS id, db2.r.title AS title FROM db1.links AS l LEFT OUTER JOIN db2.links AS r ON (r.link_id = l.link_id) WHERE l.link_id IN ($ids)';
Why will it not allow me to use title AS title etc for both databases? Is there a way I can do this?
You can't have 2 columns with same name in a SELECT list. That's why you can't have two id or two title.
If you want to join the two tables, use:
$CONF['mysql_query'] = "
SELECT l.link_id AS idL
, l.title AS titleL
, r.link_id AS idR
, r.title AS titleR
FROM db1.links AS l
LEFT OUTER JOIN db2.links AS r
ON (r.link_id = l.link_id)
WHERE l.link_id IN ($ids)
";
The above will show all ids and titles that satisfy the l.link_id IN ($ids) condition from the first table and the corresponding title from the second table. NULLs in the 3rd and 4th column will show if there is no corresponding row in the second table.
If you want a UNION, e.g. all rows from the first table (that satisfy the l.link_id IN ($ids) condition) plus all rows from the second table, use:
$CONF['mysql_query'] = "
SELECT link_id AS id
, title AS title
FROM db1.links AS l
WHERE l.link_id IN ($ids)
UNION
SELECT link_id AS id
, title AS title
FROM db2.links AS r
";
This will possibly show rows with same id, one from each table. Or even duplicate rows. Duplicate rows can be avoided easily though, by putting UNION ALL instead of UNION.
use alias title in single quotes, it will allow you
select t1.Title as 'title' , t2.Title as 'title' from Table1 t1,Table t2