MySQL Search Query for WordPress Database - mysql

I am currently building a mobile(Android) app that is a search engine for products. The products information, is currently stored in a WordPress eCommerce store(WooCommerce). I have added 5 sample products to this store for testing purposes.
I currently have the search function working inside the app. The MySQL query used for this search is as follows:
SELECT a.*, b.guid AS img_url
FROM wp_posts AS a
LEFT JOIN wp_posts AS b ON a.ID = b.post_parent
WHERE a.post_type='product'
AND a.post_title LIKE '%$search%'
Now, when I search for something like "sample", I get my search results with all of the products that contain the word sample in it's title.
However, if one of those products has more than one image attached to it. I get results for as many images that are attached to the product.(see screenshot below)
Screenshot: http://cl.ly/M7Ap
If anyone could help me with this, so that I don't get the multiple results for the one post. It would be greatly appreciated.

If you don't care about which result, but just want one per post, can you not just use GROUP BY?

Related

Report by Product Variations in Woocommerce using phpmyadmin for a SPECIFIC product

I want to generate a query in phpmyadmin that will give me a report of how many of a particular product (variation) that have been ordered. To get the basic report of any variation that has been ordered, I can run:
SELECT `meta_value`, COUNT(*) FROM `wp_woocommerce_order_itemmeta` WHERE `meta_key` LIKE 'pa_size' GROUP BY `meta_value`
This works (for now). But when I have another product later on, I want to be able to run a similar query that will limit my results to one particular product (the unique field for this is "_product_id", which is also in the same meta_key column as the pa_size). So basically I want a query: "output all the variations as a list AND do so for this particular product only."
Here are some sample lines that show these product variations together:
I know this is super old now, and you probably figured it out by now... but man, I needed this too and the query is just far too complex... then I remembered the new WooCommerce Admin dashboard plugin: https://wordpress.org/plugins/woocommerce-admin/
This has the exact thing I needed. Reporting on a product level broken out by variation.
Hope that helps someone else who may come across this thread!

Unique records in search query including many to one subquery

First time poster and enthusiastic Access newbie.
I've got a search screen based on Allen Browne's wonderful search in vba (http://allenbrowne.com/ser-62.html). This has worked great for most of my purposes, but now a child table is duplicating records.
Our clients(providers), can be enrolled in multiple programs. we've got four. I want a search that let's me filter by provider type, but not create duplicate records when a provider is enrolled in more than one provider type. In the example image, carmen titus is in the LEHRC and fccn programs, and therefore shows up twice. Tried to post pic but no dice.
Please help! I searched diligently and could not find a solution. I'd appreciate the support or to be pointed to a related post. I hope this makes sense. I think half my battle as a self-trained newbie is not knowing the terminology.
We'll need more info!
It sounds like the query you are basing the search on contains columns from two tables with a one to many relationship ie clients and "Client programs", such that a single client has zero to 4 programs.
It sounds like you only want to return a list of providers (ie rows on the one side), but your SQL is returning data from both tables.
Here's what you SQL might need to look like to do what you need:
SELECT *
FROM clients AS mainClient
WHERE
EXISTS
(SELECT 1
FROM clients AS C
LEFT JOIN ClientPrograms AS CP
ON C.ID = CP.ClientID
WHERE mainClient.ID = C.ID
' the above line links the EXISTS "Sub query" to the main query
AND client name like "*j*" ... etc...
... ie lots of criteria generated by you popup search criteria dialogue)
)
By adding the EXISTS statement the main query will be editable.
If you had used SQL like the following you would not be able to edit it
SELECT c.name, c.dob, etc.. ie all the field you want on the form whichwill all be from the client table
FROM clients AS C
LEFT JOIN ClientPrograms AS CP
ON C.ID = CP.ClientID
WHERE mainClient.ID = C.ID
' the above line links the EXISTS "Sub query" to the main query
AND client name like "*j*" ... etc...
... ie lots of criteria generated by you popup search criteria dialogue)
GROUP BY all the field in the select statement
I hope this gives you some inspiration

Mysql Query Help to compare multiple urls

I have a db with 14k links, im trying to find each url that is in the db twice or more.
Using this query below I'am able to strip them down to simple url's.
SELECT SUBSTRING_INDEX(url,'/',3) FROM Links WHERE url REGEXP '^[^:]+://';
I'm looking for help in now taking the results and counting them to present a list of all the domains that have 2 or more entries in my DB:
Once you strip down all the URLs to their simple form you can use the following query to find the no. of counts of the URLs appearing in the db.
SELECT *
FROM `Links`
GROUP BY url(`url`)
HAVING COUNT(*) > 1

MySQL query - single product with multiple reviews

Thank you in advance for any help you may be able to offer!
I'm working with an a bit of an odd database where products are related via tags and are not hierarchical.
I'm trying to select a single product using a SKU number from a table and join it with a table of product reviews like so:
SELECT ims.master_sku, ims.title, ims.price,
ims.description, ir.mvp_number, ir.title,
ir.review, ir.rating, ir.created_on
FROM default_inventory_master_skus AS ims
JOIN default_inventory_reviews AS ir
WHERE ims.master_sku = '22284319'
GROUP BY ir.review;
This gives me around 150 rows - which are all the same product but contain different reviews. My question is how can I return just the one product (as a single row) and somehow convert the reviews into columns associated with that one product?
Again - thank you for your time and help.
Rich
You can do that, although it's not "relational".
Looks like someone wants this data in Excel ;).
With MySQL, you will need to generate an SQL statement and execute it. Either within MySQL (in a procedure) or outside (e.g., in PHP). Query first for the pivot column names, put together the statement, then execute it.
An idea of the implementation is here:
http://www.artfulsoftware.com/infotree/queries.php#78

Search page engine PHP with MYSQL database?

I'm going to generalize this question so that other people can use the answers.
Let's say I have a website driven by a MYSQL database.
Database contains 5 tables:events,news,books,articles,tips.
Every table has among others 2 fields Title and Details in which I want to search
On every page of the site I have a search form (text field and button).
After I type a word or phrase I want to be redirected to a page called search where I should see the results as a list with links from the entire database.
e.g.
Book X (link on it to the book found in the database)
Event Y
Article Z
HELP: The tables are INNODB ENGINE so full text search didn't work also I'm having trouble in building a SELECT statement for searching multiple fields from multiple tables with LIKE. I've succeded with one table but with multiple tables and multiple fields I'm getting error or no data or duplicated data in some cases. Some help with this Select statement please.
Question: How do I build a search engine for all the tables in my MYSQL DB? Some SQL injection or other hacking prevention advice would be appreciated also.
My Approach to the situation is create a view based on all the tables with similar columns ( columns which we need to search only) and one more alias column with their table names/ entity name (Books, event etc)
It should look like this
EntityName Title Details
Books xxxx xxxxx
...............................
I am not explaining how to create views with union (dont use Union All if not expecting duplicates).
The next stop would be search using like statements
select * from vwSearchData where Title like '%keyword' or details like '%keyword'
Next step is to display the data along with their entity names.
Ofcourse, you need to get the keyword by filtering with html entities from the search form.
You can use UNION:
(SELECT * FROM events WHERE title LIKE '$key' OR details LIKE '$key')
UNION
(SELECT * FROM news WHERE title LIKE '$key' OR details LIKE '$key')
and so on.