![]() |
Mysql query
My mind is completely blank right now, so please help me out here :)
Table: sponsor | keyword | banner sponsor1 | horizontal | banner1.jpg sponsor1 | vertical | banner2.jpg sponsor1 | button | banner3.jpg sponsor2 | horizontal | banner4.jpg sponsor2 | vertical | banner5.jpg sponsor3 | horizontal | banner6.jpg sponsor3 | vertical | banner7.jpg sponsor3 | small | banner8.jpg Sponsor1 has: horizontal, vertical and button banners Sponsor2 has: horizontal, vertical Sponsor3 has: horizontal, vertical and small Now I want to make a query that gets me this info: " Find a sponsor that has horizontal and vertical and (button or small) " I made a quick and dirty to find sponsor that has horizontal and vertical and ..... banners. SELECT COUNT(DISTINCT keyword) from table WHERE keyword LIKE 'horizontal' OR keyword LIKE 'vertical' If I was looking for 2 kinds of banners and the query returned a 2, BINGO! I'm at a loss how to do it differently. So if anyone got a different way and a solution to the first query, I'm all ears :) |
I think I might've solved it.
UNION ! Why have I never used that before? |banghead| |
SELECT count
FROM table WHERE keyword = 'horizontal' AND keyword = 'vertical' AND (keyword = 'small' OR keyword = 'button') That is to find only sponsors that have horz and vert and butt/small baners. It must match all three conditions to be a match. Is that what you are looking for? Not sure why your using UNION, that is usually for selecting from different tables, if I remember right. Also LIKE is use more for a wildcard type select with % as the wild card. ronnie |
Quote:
If I would have 3 keyword fields: keyword1, keyword2, keyword3 then I could do it that way. And LIKE is just an old habit I have since the Sybase days. Mysql isn't as picky when it comes to =/LIKE, if you just put it in quotes it will treat it as a string. But for standard SQL, LIKE is for strings and = is for numerics. |
Thought you were trying to match rows, not tables, that would help..:) If multiple tables, JOIN might be a solution. Unless you already have it figured out.
As for LIKE, guess I am old school, proper coding so I dont run in to trouble later. ronnie |
Quote:
I have it pretty much figured out, but it's in 2 queries. #1. Get all sponsors #2. Check if sponsor has the type of banners I'm looking for SELECT 3 = COUNT(*) FROM ( SELECT DISTINCT 1 FROM banners WHERE sponsor LIKE 'sponsor1' AND keyword LIKE 'horizontal' UNION ALL SELECT DISTINCT 1 FROM banners WHERE sponsor LIKE 'sponsor1' AND keyword LIKE 'vertical' UNION ALL SELECT DISTINCT 1 FROM banners WHERE sponsor LIKE 'sponsor1' AND (keyword LIKE 'small' OR keyword LIKE 'button') ) AS tbl 3 is how many different banners I'm looking for. I just wish I could get rid of step #1 and just make it into one query. |
remove the first query and read mysql_num_rows() ?
|
Hey, if it works, thats all that counts. Kinda confusing, dont know you db layout, ect, not that I want to know.
I try to keep all my stuff in one table at a time, sure makes queries easier..:) ronnie |
Quote:
|
Quote:
Which means: If I have 100 sponsors = 101 queries. |
Here's what the full query looks like:
PHP Code:
|
mysql> create temporary table blah SELECT sponsor FROM banners group by sponsor,if(keyword='horizontal','H',if(keyword='vertical','V','O'));
Query OK, 5 rows affected (0.00 sec) Records: 5 Duplicates: 0 Warnings: 0 mysql> select sponsor,count(*) from blah group by sponsor; +----------+----------+ | sponsor | count(*) | +----------+----------+ | sponsor1 | 3 | | sponsor2 | 2 | +----------+----------+ 2 rows in set (0.00 sec) Is that what you want? |
Hmm, let me give that a try.
|
F I N A L L Y !
SELECT sponsor, COUNT(keyword) AS cnt FROM ( SELECT sponsor, keyword FROM banners WHERE keyword LIKE 'horizontal' UNION SELECT sponsor, keyword FROM banners WHERE keyword LIKE 'vertical' UNION SELECT sponsor, keyword FROM banners WHERE (keyword LIKE 'small' OR keyword LIKE 'button') ) AS tbl GROUP BY sponsor HAVING cnt = 3 1 query |thumb Thanks Sparky, you got me on the right track there with the count(). Thanks :) |
Wow yall went through some contortions
on a pretty straightforward query. It's called a self join. Oh, and BTW, like is for pattern matching. You want to use = when looking for exact matches. The query is kind of long just because the question itself is long, with several parts, but it's very simple. To understand how it works, let's first just see how to get sponsors with either horizontal or vertical: Code:
SELECT sponsors.sponsor that doesn't assume distinct by default. So we've seen how to do a self join, that is how to join a table to itself, which is just another case of relational multiplication (cartesian product) like any other. Now we just need to multiply by a third relation to get the "button or small" part: Code:
SELECT sponsors.sponsor untested and my contain typos or logical errors. (Fully tested code requires that I test it on your system.) |
Hi Raymor,
It worked to get 1 type of banner, but when I tried with 2 it returned nothing. I'll play around with it a little bit. Thanks :) |
Rather than just returning it should have given you an error.
I used a non-existant table because I accidently tried to select from just sponsors_b instead of saying "sponsors AS sponsors_b". Correct code is: Code:
SELECT DISTINCT sponsors.sponsor |
Niiice. Now it's working, I'm not sure what I made wrong before, but it works now.
The query is heck of a lot faster than having a bunch of sub-queries. Thanks man |thumb |
All times are GMT -4. The time now is 03:47 PM. |
Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
© Greenguy Marketing Inc