What am I doing wrong with this:
$sql = "SELECT * FROM content
WHERE threadName LIKE '%$filter%'
ORDER BY lastUpdated desc
UNION SELECT *
FROM content
WHERE threadName NOT LIKE '%$filter%'
ORDER BY lastUpdated desc";
The first statement before the UNION works well on its own, but this one above returns:
mysql_fetch_array() warning - supplied argument is not a valid MySQL result resource
Am I right in believing that UNION will not return duplicate entries, in which case the second SELECT statement doesn't need to have the NOT LIKE but will just return everything that wasn't listed in the first statement.
EDIT: This query should get you the rows matched by filter first, followed by those not matched:
SELECT *
FROM content
ORDER BY
CASE WHEN threadName LIKE '%$filter%' THEN 0 ELSE 1 END,
lastUpdated DESC
Note that you should never SELECT *, list the necessary columns instead.
While using UNION we must use column names instead of '*'.
Here, I m going to create .csv file at specific location on the system with emailid validation expression by using mysql query as bellow.
select 'col1','col2','col3','col4' from tableName
UNION
SELECT col1,col2,col3,col4 FROM tableName WHERE col4 NOT REGEXP '^[a-zA-Z0-9][a-zA-Z0-9.-]*[a-zA-Z0-9.-]#[a-zA-Z0-9][a-zA-Z0-9._-]*[a-zA-Z0-9]\.[a-zA-Z]{2,4}$'
INTO OUTFILE '/home/sachin/mysqloutput/data.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY ''''
LINES TERMINATED BY '\n';
Related
I have a database with TV Guide data, and in my description field (VARCHAR) sometimes i have a '|' where behind it is the rating. I used to check this in php, before converting it all to XML, but i would like to do this in SQL.
So if i have this string:
This is the description | rating pg-13
Then i want to keep the
This is the description
but if there is no '|' i want the whole string.
I tried using substring, but can't get it to work.
My query now is:
SELECT *, SUBSTRING(`long_description`, 1, POSITION('|' IN `long_description`)) FROM `programs` WHERE station_id = 1
this works only one way - this gives me the string before the '|' but if there is no '|' it gives an empty column.
Based on the use of backticks, you might be using MySQL. If so, substring_index() does exactly what you want:
select substring_index(long_description, '|', 1)
How about this:
SELECT
*,
IF(long_description LIKE '%|%',
SUBSTRING(`long_description`,
1,
POSITION('|' IN `long_description`)),
long_description)
FROM
`programs`
WHERE
station_id = 1
The IF clause basically just checks if you have a | in the field and applies your routine when this is true. Else it will simply return the complete long_description value.
SELECT DISTINCT example_type FROM Table_name WHERE 80 IN (example_type);
The values I'm using are 66 and 80.
The values for the column are sometimes "66,80" and sometimes "80,66".
If it is a search for 80, it will only return a match if the column is "80,66" (or "80").
If it is a search for 66, it will only return a match if the column is "66,80" (or "66").
The column type is set to TEXT.
The following works as expected: (both are a match)
....WHERE 80 IN (66,80)...
....WHERE 80 IN (80,66)...
I'd like to keep the contents of the column as CSV.
What do I need to do to the MySQL query to get it work no matter the order of the CSV?
May be, you can use the following where clause:
WHERE column_name like '%80%'
If your two queries are right, then you can use union clause in mysql
SELECT ... FROM ... WHERE (your first condition)
UNION
SELECT ... FROM ... WHERE (your second condition)
SELECT DISTINCT example_type FROM Table_name WHERE FIND_IN_SET(80, example_type);
This is based on the final code:
WHERE ...a.is_special = 1
AND (a.foo_list = '' OR FIND_IN_SET($foo_id, a.foo_list))
AND (a.bar_list = '' OR FIND_IN_SET($bar_id, a.bar_list))
AND (a.baz_list = '' OR FIND_IN_SET($baz_id, a.baz_list))";
I have two select statements which are as mentioned below
SELECT 'MY OUTPUT'
SELECT * FROM MY TABLE
On execution MY OUTPUT is printed first and then there is a gap for next select.
I want to use something like UNION to combine two statements.
I'm using:
SELECT 'MY OUTPUT' UNION
SELECT * FROM MY TABLE
But, I am getting error:
All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.
SELECT 'MY OUTPUT' # this query return one column
UNION
SELECT * FROM MY TABLE # this query return more than one column
number of columns must be the same
SELECT 'MY OUTPUT' UNION # return one column
SELECT column1 FROM MY TABLE # return one column now it will work
but i think you want to do this no?
SELECT 'MY OUTPUT',column1,Column2,column3 FROM MY TABLE
So here's what's happening:
The error you're getting (All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.) is because there are more columns in one select than the other.
You could use the same blank columns in your first query as there are in [My Table] which would make your query look like:
SELECT 'MY OUTPUT' , '', '' ,'', '' --(no. of columns should match those in MY TABLE)
UNION
SELECT * FROM [MY TABLE]
I'm guessing you want an excel style cell merge which is not possible as the output of a select query unfortunately.
I have a following table
SNo Data
1 |AA|B|C|D|E|
2 |AB|B|C|D|
3 |AA|C|
4 |AA|
5 |AA|AB|AC|C|
6 |AB|B|C|
data is delimited by "|". I understand that the table is denormalized but I cannot change the schema.
The user will give one more more inputs. For example if the user gives input as AA and C I have to retrieve only those rows where only AA and C occurs and not other rows
In this case my output will be
SNo Data
3 |AA|C|
The query that I have tried
Select * from table1 where data like %AA%C% will retrieve rows 1,2,3,5,6
Thanks
Assuming that within a record:
"blank" values are not to be ignored—i.e. |AA||C| is not the same as |AA|C|; and
none of the values are repeated—i.e. |AA|C|AA| will never occur.
Then you can perform pattern matches and test the total value length:
SELECT SNo
FROM my_table
WHERE data LIKE CONCAT('%|', 'AA', '|%')
AND data LIKE CONCAT('%|', 'C' , '|%')
AND CHAR_LENGTH(data) = 1 + CHAR_LENGTH('AA')
+ 1 + CHAR_LENGTH('C' )
+ 1
See it on sqlfiddle.
You can use wildcards, but you have to separate the expressions like this:
SELECT * FROM table1
WHERE data LIKE '%|AA|%' AND data LIKE '%|C|%'
This gets you all the records that contain both |AA| and |C|. However, to limit to rows that match only both:
SELECT * FROM table1
WHERE data LIKE '%|AA|%' AND data LIKE '%|C|%'
AND LENGTH(data) = 6
If you have just two, you may as well key them in and actually use an index on the data column though:
SELECT * FROM table1
WHERE data = '|AA|C|' OR data = '|C|AA|'
Which on older versions of MySQL can be optimized to:
SELECT * FROM table1
WHERE data = '|AA|C|'
UNION ALL
SELECT * FROM table1
WHERE data = '|C|AA|'
Alternatively, you could use a regular expression, which would not utilize an index.
A little idea using regular expressions:
select * from table1 where data regexp '(\\|AA\\|)+.*(\\|C\\|)+';
If I have understood correctly, what you are asking is "get the rows which matches input with delimiter".
So if input is "AA" and "c" then get rows where Data column will have result exactly like
|AA|C|
You can probably do like
select * from table1 where data = select concat('|',concat_ws('|','AA','C'),'|')
$whereClause = array();
foreach( $input AS $search ){
$whereClause[] = sprintf("data REGEXP '|?%s|?'", $search);
}
$sql = "SELECT * FROM tbl WHERE ".implode(" AND ", $whereClause);
Regular Expression Explained
This is the regular expression '|%s|' .
I have successfully exported my table in MySQL to csv with columns. I used this guy's answer at Include headers when using SELECT INTO OUTFILE?
SELECT 'ColName1', 'ColName2', 'ColName3'
UNION ALL
SELECT ColName1, ColName2, ColName3
FROM YourTable
INTO OUTFILE '/path/outfile'
However, I want to export a query formula as a new column to be added to the csv file. I tried adding an extra calculated column after the second SELECT statement. MySQL gave me an error saying "The used SELECT statements have a different number of columns".
Example formula: SELECT CAST((ColName1 * ColName2) AS DECIMAL(7,2)) AS ColNameX. I'm not sure where to input it in my export statement.
For the UNION to work you must have the same number of columns and they must be of the same type. As you creating the header row as text then all of your columns in the second query must also be text. Like so:
SELECT 'ColName1', 'ColName2', 'ColName3', 'New Column'
UNION ALL
SELECT
ColName1
,ColName2
,ColName3
,CAST(CAST((ColName1 * ColName2) AS DEC(5,2)) AS CHAR)
FROM YourTable
INTO OUTFILE '/path/outfile'