How to create sql select by 3 level expression and statement
Normally, my website based on SQLite database and the search result will be display by $sql =
"SELECT DISTINCT * FROM amz WHERE Title LIKE \"$qq%\" OR Price LIKE \"$qq%\" GROUP BY Title";.
Above will be search and select query that contains any search keyword from database in column Title or Price.
However, I need to create 3 expression and statement from database as:
Default will be search and display result as $sql = "SELECT DISTINCT * FROM amz WHERE Title LIKE \"$qq%\" OR Price LIKE \"$qq%\" GROUP BY Title";
If can't find any search result from Title and Price column. Then SQL will be check in Category column as $sql = "SELECT DISTINCT * FROM amz WHERE Category LIKE \"$qq%\" GROUP BY Title";
Finally, if not match in each column. SQL result will be echo custom message.
I try to create with myself. But it seems the result echo 1.) option only.
Example:
I search “bedding comforter set”. And in column Title in database had products contain this keyword without (s).
Initially, an expression will be check if it match 1). A statement will do selecting products from column Title or Price.
However, in column Category. I declare specific category to each product such as “bedding comforter sets” to Category.
So, if search not match bedding comforter sets in Title or Price. The result will be display products that match search query of bedding comforter sets in column Category instead Title or Price.
Finally, if not search query not match any in each column will be echo custom message.
Additional:
This is a screebshot comparing between query 1 and query 2 result
This screenshot is query 1 result that search keyword without (s) and like or match in Title column or Price column from database.
This screenshot is query 2 result that search keyword with (s) which like or match in Category column. This strange because it turns as Not Found while I set it to display result.
And this is my coding that I not sure it correct or not?
Thanks
Regards
Is there a requirement to write one sql to accomplish this?
You have essentially written pseudo-code:
Default will be search and display result as $sql = "SELECT DISTINCT * FROM amz WHERE Title LIKE \"$qq%\" OR Price LIKE \"$qq%\"
GROUP BY Title";
If can't find any search result from Title and Price column. Then SQL will be check in Category column as $sql = "SELECT DISTINCT * FROM
amz WHERE Category LIKE \"$qq%\" GROUP BY Title";
Finally, if not match in each column. SQL result will be echo custom message.
Now start expressing it in language the machine understands. A starting point:
execute and fetch result from query 1
if result set is not empty, display result
else execute and fetch result from query 2
if result set is not empty, display result
else display custom message
--EDIT --
Based on screenshot of code, you are not running the mentioned queries at all, but some other query modified by the str_repl. Which seems to morph it into an aggregate query (COUNT(*)), and aggregate queries always return at least one row. That could cause this problem.
Related
I am trying to show something similar to related products on on my website. I have tested the following query, and I get no errors; but I get no results either.
<?php
$relatedStmt = $db->query("SELECT * FROM items WHERE tag LIKE id = 1 LIMIT 3");
?>
I also tried %1% and it displayed ALL the results, I assumed the code was thinking the query was just SELECT * FROM items.
The tags in the column are displayed in the following format: tagone two three four, so I am trying to display the products that have similar tags, hence why I used the LIKE clause.
Table screenshot
The current query is to relate with this query: SELECT * FROM items WHERE id = 1. So LIKE id = 1 is to find the tags that match this query to show the related products.
LIKE doesn't work the way you seem to expect. It's a character for character comparison, with wildcards, between the operands.
Of the mysql functions, closest to what you want is probably LOCATE or FIND_IN_SET. There are split solutions in other questions, e.g. "Can Mysql Split a column?" and "Split a MYSQL string from GROUP_CONCAT into an ( array, like, expression, list) that IN () can understand". However, even so there is no good way to compare the individual tags in your concatenated tag column with the individual tags of the other rows in your table.
Therefore, I think you'd be better off moving the tags into their own table with item_id as a foreign key. Then the solution is trivial (SQLFiddle):
-- Related Items
SELECT *
FROM items
WHERE id in (SELECT DISTINCT item_id
FROM tags t
JOIN (SELECT tag
FROM tags
WHERE item_id = 1
) t1 ON t1.tag = t.tag
WHERE item_id != 1
)
;
i am unsure about that id = 1 thing in your query but anyway you could try this: SELECT * FROM items WHERE tag LIKE '%search_this_tag%' LIMIT 3
I was writing a products search engine for a pet project website and ran into a problem. How do I return products ordered by "how much of a match" they were for the query? Right now I have this, which will only return products where the query matches the beginning.
$sql = "SELECT * FROM `Products` WHERE name LIKE '$query%'";
Placing a % in front of $query like so
$sql = "SELECT * FROM `Products` WHERE name LIKE '%$query%'"
achieves the desired output but it is not ordered correctly. For example, the user searches for "p" and they get a list like: [A-word with p in it], [B-word with p in it], [P-word]. I would like the P-word to show up first, and the A & B words to show up after it. Is this possible with only MySQL? I've looked into ORDER BY but that orders by columns AFAIK.
You can do this with order by:
SELECT *
FROM `Products`
WHERE name LIKE '%$query%'"
ORDER BY (name like '$query%') desc,
name;
The first clause will put the names that start with $query first.
We have six categories Example :- a,b,c,d,e,f.
In each category we have products.
In a category i have 2 products & in b category i have 1 product.
I'm fetching this data from database.
product_id category_id
1 1
2 1
3 2
What should be best logic to display records, so two categories not display next to it.
Output product id like :- 1,3,2
I do not think such function exists. Even if you use DISTINCT it will only select the ones that do not duplicate and leave the out the rest, which is not what you need. So, I think I would run two queries at the same time, the first one will choose all the distinct queries, like 1,2,3,4 and after that, you can create a second query just to query normally all products. This way, let say.. if you had products by type like 1,1,2,3,3,3,4,4,4,4,5,5,5, then, the first sql query would list 1,2,3,4,5 and the second will just query normally, but still, your clients will see the distinct ones first.
SELECT DISTINCT type FROM table ORDER by id
I have this query but it returns the name of author the number of times it exists in the database ..
$query = "SELECT bauthor FROM info WHERE Cat1 = 'novel'";
$result = MySQL_query($query);
i want the author's name to be displayed once and the number of books he has to be in a bracket ...for example author's name is aaaaa and he has written 20 books so wen i run this query it shows mw his name 20 times but i want it to be in this way aaaa(20)
I can't do much without your full schema, but try using the COUNT feature with a GROUP BY clause, like
SELECT bauthor, COUNT(books) AS numbooks FROM info WHERE ... GROUP BY bauthor
EDIT: See this SQLFiddle for an example: http://sqlfiddle.com/#!2/bb5f5/1/0
I have read about "slugs" but I still can`t figure out how to use them. How to properly save slugs in database ?
Lets say i have url like this http:/example.com/samsung/samsung_continuum_android_smartphone /
This is what I do
select category_id from categories where category_name = 'samsung'
After that I do query like
select slugs from my_table where category_id = (lets say 3 where is samsung)
Result is something like
samsung_continuum_android_smartphone
samsung_some_other_phone.
After that I can do something like this
select * from info where title = 'samsung_continuum_android_smartphone'
to get the information about the product. Is this the right way to do it ?
Your going a little over board, slugs are the exact same principle as unique identification numbers, the only difference is that a slug represents natural language for ease of reading as well as SEO.
Now I see that your performing 2 queries to get the row that matches the slug, where as you should only select 1 row from your info where the slug matches the container.
For instance:
SELECT * FROM categories WHERE category_slug = 'samsung';
the category_slug would be just as unique as category_id, thus removing any conflict's that may occur.
now if you construct your table scheme so that the slugs are only uniq to the category, this would remove conflicts but each slug for a post would need to be unique for the category, you can then select the correct post from the post table filtered by the category.
SELECT * FROM posts WHERE category_id = (SELECT category_id FROM categories WHERE category_slug = 'sumsung') AND WHERE post_slug = 'this_would_be_the_slug_for_the_post'
both of these values can be pulled from $_GET and placed into the above SQL String.