Formatting the output using select statement - mysql

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.

Related

Nested select statement in mysql -my code doesn't work

here is my query
SELECT con_serial,column2,column3
FROM
(SELECT con_serial,column2,column3
FROM big_table
WHERE ISNULL(contact1, '')+'#'+ISNULL(contact2, '')+'#'+ISNULL(contact3, '')+'#'+ISNULL(contact4, '')+'#'+ISNULL(contact5, '')
LIKE '%' + '".$conserial."' + '%') AS a
WHERE con_serial
IN('".$contact1."','".$contact2."','".$contact3."','".$contact4."','".$contact5."')
at the inner select i wish to get the rows which have this value $conserial in one of their 5 columns(contact1...contact5)
and the outer select to choose the rows from it that their column con_serial is one of the variables ($contact1...$contact5)
can anybody see what's wrong here?
Despite your new formulation, it remains very much unclear.
Nevertheless I'll try to give you an answer, based on what I can guess...
First here is how I'd reformulate your need:
you have some values in PHP variables: one $conserial and five $contact# where # is 1-5
the table structure contains at least these columns: con_serial, column2, column3, and five contact# where # is 1-5
you want to select rows where both (here is the most strange part of your need):
at least one of the contact# columns matches the PHP $conserial value
the con_serial column matches at least one of the PHP $contact# values
That said, note that you don't need to have two nested SELECT: you only want each row to satisfy two conditions, so they can be ANDed in the WHERE clause.
Based on that, your query should be:
$query = "
SELECT con_serial, column2, column3
FROM big_table
WHERE con_serial IN ('$contact1', '$contact2', '$contact3', '$contact4', '$contact5')
AND '$con_serial' IN (contact1, contact2, contact3, contact4, contact5)
";
i guess the sum part in where clause is nut allowed
any way i've solved this with using two IN like this
SELECT con_serial,,column2,column3
FROM(SELECT con_serial,column2,column3
FROM
big_table
WHERE '".$conserial."' IN(contact1,contact2,contact3,contact4,contact5)) a
WHERE con_serial IN('".$contact1."','".$contact2."','".$contact3."','".$contact4."','".$contact5."'
this was wat i wanted Tnx any way ;)

Searching for number in mysql column with CSV

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))";

MySQL get all characters before specific character

I have a table where I extract some values, one column values can contain "value1|value2|value3", but I only want to get the characters before the | - "value1".
This is what I tried, but it doesn't work.. What am I doing wrong?
Thanks!
$sql = "SELECT * LEFT('Like', LOCATE('|', 'Like')-1) FROM $tablename
WHERE Parent = '0' AND Type LIKE 'top' ORDER BY Order ASC";
I want to use this for ALL values, not just one field..
you need the following statement to get that portion of [ColName]:
LEFT([ColName],INSTR([ColName],"|")-1)
If you want to select multiple columns into the same recordset column you can union all with something like the following:
SELECT LEFT(ColName,INSTR(ColName,"|")-1) AS FirstValue From $TableName;
UNION ALL
SELECT LEFT(ColName2,INSTR(ColName2,"|")-1) AS FirstValue From $TableName;
If you want to use this on multiple columns, script the creation of the sql.
Two things: (1) you don't have a comma between your * and the expression you're trying to do with LEFT and (2) you're putting like in quotes, so the functions are working on the constant value like instead of your column named like. Try putting like in backticks.
SELECT *, LEFT(`Like`, LOCATE('|', `Like`)-1)
...
You can also use the MySQL SUBSTRING_INDEX function for this:
SELECT *, SUBSTRING_INDEX(`Like`, '|', 1)
...

Add extra columns for MySQL export to csv?

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'

MySql Select rows where value IN comma-delimited column

I'm trying do something like this:
SELECT * FROM table WHERE column IN (1,2,3)
but in place of 1,2,3 I want to use a column from another table that contains a comma-delimited list just like "1,2,3" above.
I have tried to do this:
SELECT
GROUP_CONCAT(a.eating_area SEPARATOR ', ')
FROM table_areas a
WHERE a.eating_area_id IN (
SELECT
o.eating_area_ids
FROM table_offers o WHERE o.rid=1
)
however this only returns the value associated with 1, and not 2 or 3. Can this be done or is there another way to do this?
Many thanks
SELECT * FROM table t
WHERE IF(FIND_IN_SET(column,(SELECT "1,2,3" FROM otherTable WHERE 1))>=1,1,0)
-- FIND_IN_SET will return the position.
I don't know if it's the best way to do it but... i think it could work.
Source: Find_in_set