SELECT FROM solarbricklight, solarcharger, solarlantern, solarled, solarlightfan, solarlightingkits
WHERE ="productid AND productname"
ORDER BY productid
Your question is not very clear.
But still let me try. Assuming that theres productid and productname in each of your tables (for purpose of an example I am assuming that there are three tables t1, t2 and t3). Heres what you will do...
$query = "(SELECT productid, productname, 't1' as type FROM t1 WHERE productid = 1 OR productname LIKE '%" . $keyword ."%') UNION (SELECT productid, productname, 't2' as type FROM t2 WHERE productid = 1 OR productname LIKE '%" . $keyword ."%') UNION (SELECT productid, productname, 't3' as type FROM t3 WHERE productid = 1 OR productname LIKE '%" . $keyword ."%')";
mysql_query($query);
So, you are getting result from all of the three tables, and you can identify which row came from which table by looking at its type value.
Looks like you should thing about a database design like this:
TABLE PRODUCTS
product_id (int autoincrament)
product_name (varchar(50))
Then you SQL can be like :
SELECT product_id, product_name FROM products WHERE product_name LIKE "%search_string%"
ORDER BY product_id ASC
If you want to search from six tables independently, then use union all:
SELECT
FROM solarbricklight
WHERE ="productid AND productname"
UNION ALL
SELECT
FROM solarcharger
WHERE ="productid AND productname"
UNION ALL
SELECT
FROM solarlantern
WHERE ="productid AND productname"
UNION ALL
SELECT solarled
FROM olarlightfan
WHERE ="productid AND productname"
UNION ALL
SELECT
FROM solarlightingkits
WHERE ="productid AND productname"
ORDER BY productid;
You need to be sure the columns in the select clause are all compatible (and all have the same number). The final ORDER BY refers to the results of the entire query.
Here are some tutorials on database designs - read this, then start designing your database :)
http://www.ntu.edu.sg/home/ehchua/programming/sql/Relational_Database_Design.html
http://www.datanamic.com/support/lt-dez005-introduction-db-modeling.html
A beginner's guide to SQL database design
Related
$query = mysqli_query($mysqli, "SELECT product_name ,MIN(product_price) product_price,link FROM(
select jumia.product_name,jumia.product_price,jumia.product_image,jumia.link from jumia INNER JOIN
jumia ON jumia.id = olx.id
where product.name like '%{$search}%'
UNION
select olx.product_name,olx.product_price,olx.product_image, olx.link from olx INNER JOIN
olx ON olx.id = jumia.id
where product.name like '%{$search}%')Minim
GROUP BY product_name,product_image
");
I am trying to create a query from two tables with similar column names as displayed above that will allow me to display the rows between the two tables that have the lowest price.
For example, if product_name called mattresses is searched the matching item in my database whose price is lower between the two table names should be displayed. Some help would be appreciated
I think this is the general idea of what you're trying to do:
SELECT id, price
FROM (SELECT id, price FROM T1 WHERE price = (SELECT min(price) FROM T1)
UNION
SELECT id, price FROM T2 WHERE price = (SELECT min(price) FROM T2)
) AS M
ORDER BY price ASC
LIMIT 1
Ended up changing the code and removing the ordering at the end and this finally worked. I hadn't properly linked my database using foreign keys, and changed my code to reflect this.
$query = mysqli_query($mysqli, "
SELECT product_name
, MIN(product_price) product_price
, link
FROM
( select j.product_name
, j.product_price
, j.link
from jumia j
JOIN olx
ON j.categoryID = olx.categoryID
where j.product_name like '%{$search}%'
UNION select olx.product_name
, olx.product_price
, olx.link
from olx
JOIN jumia
ON jumia.categoryID = olx.categoryID
where olx.product_name like '%{$search}%'
) x
");
this is my query
SELECT * FROM (
SELECT simple_sku, picture_url, product_name, discounted_price, tracking_link, 'Lazada.co.id' AS table_identity
FROM `list_lazada`
UNION
SELECT id_product, picture_url, product_name, sale_price, tracking_link, 'Simulation.com' AS table_identity
FROM `list_simulation`
) AS big_table
WHERE product_name LIKE '%$search%' ORDER BY big_table.discounted_price ASC
LIMIT 12 ";
now develop php with phpmyadmin.
Every table has 4 coloumn only
How to make it faster?
You don't need the outer subquery:
SELECT simple_sku, picture_url, product_name, discounted_price, tracking_link,
'Lazada.co.id' AS table_identity
FROM `list_lazada`
UNION
SELECT id_product, picture_url, product_name, sale_price, tracking_link,
'Simulation.com' AS table_identity
FROM `list_simulation`
WHERE product_name LIKE '%$search%'
ORDER BY discounted_price ASC
LIMIT 12
I think your LIKE condition will render an index on the product_name column not usable, resulting in a full table scan. But maybe you can improve your search approach to take advantage of an index.
Is there a faster solution than mine?
I have a table with columns orders_id and products_id (can't change that)
Now I like to know how many orders made with unique products_id.
I tried the following. But it crashes while loading tooo many rows (160'000).
SELECT DISTINCT `products_id` , `products_name` , COUNT(*) as totalorders FROM `orders_products` ORDER BY `products_id` ASC
I tried also with subqueries, but same problem as above.
SELECT (SELECT count(*) FROM orders_products WHERE products_id = op.products_id) AS totalorders, products_id, products_name FROM orders_products op ORDER BY products_id ASC
Hopefully you have an index on products_id
In that case, I would try something like the following:
select products_id, Min(Products_Name), count(*) as TotalOrders
from Orders_Products
Group by Products_ID
Order by Products_ID asc
There are a couple of other ways that you could grab the products_name, such as including it in your grouping (some other implications there).
How about:
SELECT `products_id`, `products_name`, COUNT(*) FROM `orders_products` GROUP BY (`products_id`, `products_name`) ORDER BY `products_id`
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')
I have a table categories and table posts . I want to return categories that have more than 3 posts.
My query
SELECT `categories`.`category_title`, COUNT(posts.post_id) as total_posts
FROM (`categories`)
JOIN `posts` ON `posts`.`category_id` = `categories`.`category_id`
HAVING `total_posts` > 3
ORDER BY `categories`.`date_created` desc
it returns just 1 row.. What is the correct way to do this type of query without using 2 queries?
Your query is making use of a MySQL feature called "hidden columns" and you might not even know it. This is because your query is referencing elements, such as date_created, which should be aggregated but are not ("should" here means according to the SQL standard and most other databases).
The problem with your query is that it is missing the group by. An alternative way of writing this is with the aggregation in a subquery, before joining to category:
SELECT `categories`.`category_title`, total_posts
FROM `categories` JOIN
(select categoryid, COUNT(posts.post_id) as total_posts
from `posts`
group by categoryid
having count(*) > 3
) pc
ON `pc`.`category_id` = `categories`.`category_id`
ORDER BY `categories`.`date_created` desc
You need to group the items by category.
SELECT `categories`.`category_title`, COUNT(posts.post_id) as total_posts
FROM (`categories`)
JOIN `posts` ON `posts`.`category_id` = `categories`.`category_id`
GROUP BY `categories`.`category_id`
HAVING `total_posts` > 3
ORDER BY `categories`.`date_created` desc