Difference between revisions of "String over columns"
| Line 18: | Line 18: | ||
OR ceilingcolor = 'YELLOW' | OR ceilingcolor = 'YELLOW' | ||
OR wallcolor = 'YELLOW' | OR wallcolor = 'YELLOW' | ||
| − | <p>Using OR increases the chances for careless mistakes instead | + | <p>Using <code>OR</code> increases the chances for careless mistakes instead |
| − | CONCAT could be used to make the query more efficient.</p> | + | <code>CONCAT</code> could be used to make the query more efficient.</p> |
| − | <p>To avoid problems make sure to add in separators and if a value | + | <p>To avoid problems make sure to add in separators and if a value can be null |
| − | make sure to use it is wrapped in COALESCE | + | make sure to use it is wrapped in <code>COALESCE</code> or <code>NVL</code> for example:</p> |
COALESCE(floorcolor,' '). | COALESCE(floorcolor,' '). | ||
</div> | </div> | ||
| Line 27: | Line 27: | ||
WHERE CONCAT (':',floorcolor,':',ceilingcolor,':',wallcolor,':') | WHERE CONCAT (':',floorcolor,':',ceilingcolor,':',wallcolor,':') | ||
LIKE '%:YELLOW:%'</source> | LIKE '%:YELLOW:%'</source> | ||
| − | <div class="ecomm e-mysql" style="display: none">Instead of COALESCE or NVL, IFNULL can be used.</div> | + | <div class="ecomm e-mysql" style="display: none">Instead of <code>COALESCE</code> or <code>NVL</code>, <code>IFNULL</code> can be used.</div> |
| − | <div class="ecomm e- | + | <div class="ecomm e-sqlserver" style="display: none">Instead of <code>COALESCE</code> or <code>NVL</code>, <code>IFNULL</code> can be used.</div> |
</div> | </div> | ||
{{Hacks Ref}} | {{Hacks Ref}} | ||
Revision as of 13:38, 30 July 2012
Search for a string across columns
DROP TABLE bedrooms
CREATE TABLE bedrooms( name VARCHAR(20), floorcolor VARCHAR(20), ceilingcolor VARCHAR(20), wallcolor VARCHAR(20)); INSERT INTO bedrooms VALUES ('Jim','RED','GREEN','YELLOW'); INSERT INTO bedrooms VALUES ('Bob','YELLOW','BLUE','BLACK'); INSERT INTO bedrooms VALUES ('Allan','BLUE','PINK','BLACK'); INSERT INTO bedrooms VALUES ('George','BLUE','GREEN','OAK');
SELECT name FROM bedrooms WHERE floorcolor = 'YELLOW' OR ceilingcolor = 'YELLOW' OR wallcolor = 'YELLOW'
Using OR increases the chances for careless mistakes instead
CONCAT could be used to make the query more efficient.
To avoid problems make sure to add in separators and if a value can be null
make sure to use it is wrapped in COALESCE or NVL for example:
COALESCE(floorcolor,' ').
SELECT name FROM bedrooms WHERE CONCAT (':',floorcolor,':',ceilingcolor,':',wallcolor,':') LIKE '%:YELLOW:%'
Hack 10 Converting subqueries into joins
Hack 11 Converting aggregate subqueries into joins
Hack 16 Search for a String across columns
Hack 24 Multiply Across a Result Set
Hack 25.5 Splitting and combining columns
Hack 26 Include the rows your JOIN forgot
Hack 30 Calculate the maximum/minimum of two fields
Hack 33 Get values and subtotals in one shot
Hack 50 Combine tables containing different data
Hack 51/52 Display rows as columns
Hack 55 Import Someone Else's Data
Hack 62 Issue Queries Without Using a Table
Hack 63 Generate rows without tables
Hack 72 Extract a subset of the results
Hack 78 Break it down by Range
Hack 88 Test two values from a subquery