Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    MYSQL




UNION Function How To In MySQL 3


How to write this line in mysql3 where there is no UNION function?

(SELECT * FROM table1) UNION (SELECT * FROM table1) ORDER BY date LIMIT 0,5;




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
UNION, Individually Works Fine, But Not In Union
Well, I am using an UNION, and while both of the queries works fine while used individually, they don't work while in the UNION.

Here is the code -

PHP

(
SELECT *
FROM job
WHERE date_fin !=0000-00-00
ORDER BY date_fin ASC
)
UNION (
SELECT *
FROM job
WHERE date_fin =0000-00-00
ORDER BY date_creation ASC
)

What do u think guys?

Note : MySQL version 4.1.21

Note : Not working means, this part is not working => 'ORDER BY date_fin ASC', means, the returned results are not ordered as I want.

MySQL doc. tells,

To apply ORDER BY or LIMIT to an individual SELECT, place
the clause inside the parentheses that enclose the SELECT:

(SELECT a FROM t1 WHERE a=10 AND B=1 ORDER BY a LIMIT 10)
UNION
(SELECT a FROM t2 WHERE a=11 AND B=2 ORDER BY a LIMIT 10);

Union In Mysql 3.23
Does mysql 3.23 supports "union" It doesn't accept the syntax saying i have an error near 'UNION' i took the most simple example and still it doesn't work. Example:

SELECT Custnum
FROM Custnotes
UNION
SELECT Custnum
FROM Note_NoTipul

UNION And MYSQL
I have mysql 4.1.2 and i wrote the followng code:

select tipmodel from Comp;
union
select tipmodel from Comp2;

Because i have two tables:Comp, Comp2, with the same fields: tipmodel. But the union doesn't work. What could be the problem?

Mysql UNION
I have 2 tables with data i need to loop out to a webpage.

Table1
-Field1_ok
-Field2

Table2
-Field1
-Field2-er

I've tried using...

SELECT Field1 AS f1 FROM Table1 UNION SELECT Field1 AS f1

However, not every value in each table is outputted.

Any ideas?

Mysql Union
I have been developing a mysql application for a month on one machine, time to move to another, much to my surprise it has 3.23.58 and there is no way to change it. Now all of my union queries are broken.
I found one solution:

CREATE TEMPORARY TABLE temp_union TYPE=HEAP select * from asdf;
INSERT INTO temp_union select * from qwer;
SELECT * FROM temp_union;
DROP TABLE temp_union;

Now I ask, how efficient is this? I mostly use the union in two places: a search query where I aggregate a search from three tables, and a favorites list where I aggregrate favorites from three tables. So using the above methods I would need to create 2 temporary tables for each query.

I am using php as a frontend, would it be better to do 3 queries and aggregrate everything in php? the only reason I do it on mysql is because I also need a sorting.

Mysql Problem With UNION
I have a strange problem with a UNION of 3 queries. The 3 separate
select-queries return (at this point) respectively 4, 3 an zero rows,
but the UNION returns no rows at all.

When I take the query that returns 0 rows, and put it in front of the
other two, everything to go fine (I get 7 rows), but this is of course
no real solution, next time another query returns zero rows.

MySQL 5.0.1 : CREATE VIEW And UNION
I don't manage to create a view from the UNION of two tables.

This statement works all right :
CREATE OR REPLACE VIEW myview AS SELECT 1 AS mycol UNION ALL SELECT mycol FROM mytable2;

This one does not :
CREATE OR REPLACE VIEW myview AS SELECT mycol FROM mytable1 UNION ALL SELECT mycol FROM mytable2;

and I get the error "ERROR 1143 (42000): create view command denied to user 'root'@'localhost' for column 'mycol' in table 'myview'", although I am creating the view using the "root" account.

Version used : MySQL 5.0.1 alpha snapshot (Windows binary).

Equivalent Of 'UNION' In MySQL Syntax?
How can I express this in MySQL syntax?

(SELECT Value, Name
FROM orderTable
WHERE id = '1jn8')
UNION
(SELECT Value, Name
FROM orderTable
WHERE ct_id = '12dNOI')
UNION
(SELECT Value, Name
FROM orderTable
WHERE country = 'US').

IF Function,GROUP BY,aggregate Function Problems
Yep, I have all those problems in the title. So I'll explain each one at a time - I did have another thread relating to this very same query but I thought it was time to update where I am with the query because at the moment I feel like I am getting nowhere!

The query I have basically searches through an items_ordered table through each product and checks to see whether the item is VATable or not. This is not where I have the problem though. Where I am really having the first problem is when I am trying to use the IF function to check if the TOTAL of an order is over £300. IF it is then I multiply it my 0.95 (i.e. 5% off). With the query below I get no errors but neither do I get the desired result. It's as though it couldn't even see it. =....

Mysql Function
i am hosting my website on a hosting website server.  I made Mysql database there. I noticed one thing when i insert data in tables they have a colum called "function" with drop down box where they have values like ASCII,CHAR,SOUNDEX, UCASE, LCASE, COUNT,AVG any many other.

I don't pick any of them. But I am just wondering if I need to pick either ASCII or char?

MYSQL Function For 'or'?
In the context of a where statement, what is the correct way to say 'or' as in the following:

SELECT * FROM table WHERE field1 [or] field 2 LIKE "%$query%"

This is for a basic search feature where people are tending to misspell words, I'd like to add a search words column that will also be searched.

Mysql 4 Function From A Mysql 5 Function
I found this function on the mysql website :

CREATE FUNCTION substrCount(x TEXT, delim varchar(100)) RETURNS int
RETURN (length(x) - length(REPLACE(x, delim, ''))) / length(delim);

That works perfectly with mysql 5. But when I try to import this to mysql 4, I have an error :

suse-server:/partages # mysql mybase <function-add.sql> output.tab
ERROR 1064 (42000) at line 1: You have an error IN your SQL syntax; CHECK the manual that corresponds TO your MySQL server version FOR the RIGHT syntax TO USE near '(x TEXT, delim varchar(100)) returns int
return (length(x)-length(REPLACE(x, del' at line 1

Mysql Function
is there any function in mysql which tells you what the last row inserted into a table is?

Function In MYSQL
I am using MYSQL 5.0.22. My database name is 'normal'. It could contain 2 tables, such as Itemgroup & Itemlist. I tried with the FUNCTION query for Concatination purpose. Funtion name is Serial, The Query is,
CREATE FUNCTION Serial (s CHAR(10)) RETURNS CHAR(25) RETURN CONCAT('SALES',s,'IND');
It would say the error as followes,
ERROR 1044 (42000): Access denied for user 'root'@'%' to database 'normal'
Is the Query right or wrong? or How will I used (or) handiled the FUNCTION option?
If any one knows, please guid me

Warning: Mysql() [function.mysql]: Access Denied For User 'apache'@'localhost' (using
my script is not connecting to my database, i get this error

Warning: mysql() [function.mysql]: Access denied for user 'apache'@'localhost' (using password: NO)

Mysql IF Function Question
I am trying to incorporate an IF statement into a query so that it will show a P after a phone number if this phone is a personal cell phone. This is being used in a view that I use in multiple locations to create phone listings of all employees in the directory.
My query is:

CREATE VIEW phonelisting as select
users.UserID,
concat_ws(' ', users.fname, users.minitial, users.lname) userName,
users.positionTitle AS positionTitle,
group_concat(distinct concat('(', substr(CAST(did.DIDID as CHAR), 1, 3), ') ', substr(CAST(did.DIDID as CHAR), 4, 3), '-', substr(CAST(did.DIDID as CHAR),7 , 4)) order by CAST(did.DIDID as CHAR) ASC separator '<br />') dids,
group_concat(DISTINCT CAST(phone.ExtensionID as CHAR) ORDER BY CAST(phone.ExtensionID as CHAR) ASC SEPARATOR '<br />') extensions,
users.homePhone,
users.pager,
group_concat(distinct concat('(', substr(cell.number, 1, 3), ') ', substr(cell.number, 4, 3), '-', substr(cell.number,7 , 4)) order by cell.number ASC separator '<br />') AS cellphones
FROM
users
LEFT JOIN
cellrelationship
ON
cellrelationship.userID = users.UserID
LEFT JOIN
cell
ON
cell.CellID = cellrelationship.cellID AND cell.hideListing <> &#391;'
LEFT OUTER JOIN
phonerelationship
ON
users.UserID = phonerelationship.UserID
LEFT OUTER JOIN
phone
ON
phone.PhoneID = phonerelationship.PhoneID
LEFT OUTER JOIN
DID
ON
did.ExtensionID = phone.ExtensionID
WHERE
users.userStatus <> 'term'
AND
users.hideListing <> &#391;'
AND
(users.termdate = '' OR users.termdate >= UNIX_TIMESTAMP(CURRENT_DATE))
AND
users.hireDate <= UNIX_TIMESTAMP(CURRENT_DATE)
GROUP BY
users.UserID
ORDER BY
userName
I would like to have the P show up at the end of each phone listed(Only if it is a personal phone) on the line with:


group_concat(distinct concat('(', substr(cell.number, 1, 3), ') ', substr(cell.number, 4, 3), '-', substr(cell.number,7 , 4)) order by cell.number ASC separator '<br />') AS cellphones
The way I determine if it is a personal cell or company cell is by the value of cell.Type being Company or personal.

Any suggestions?

*I am running mysql 5.0.27

Mysql Date Function
I just want to know if this command below is acceptable in MySQL form? Because, I have made a web form processing in which the date is basing as EST time (east standard time in US) and I'm in ASIA. I just found a mysql function date thru googling..

$sql = "INSERT INTO table SET
date = SUBDATE(NOW(), INTERVAL 1 DAY)";

MySQL Have A Decode Function??
Oracle has Decode(asdf,asdf,adf,adf) etc.

does MySQL have an equivilant?

Is There Trigger Function In MySQL ?
It seems to me that there is no trigger function in MySQL but I am =
not sure.

Recently, I have a project which need the trigger function when MySQL =
database server receive a alert information and wake up some procedure. =
Is there any other method to do the same functionality of trigger as in =
ORACLE databse ?

MySQL Now() Function Problem
I am new to MySQL and my problem is with storing a datetime value in a
table. I created a table with a column designated as DATETIME. I
fill it with the NOW() function. No matter whether I default it or
try and set with code, all I get is a date! Even running "SELECT
Now()" only returns a date.

If I run the following query:

Date_Format(now(), GET_FORMAT(DATETIME,'USA'))

it returns a datetime value. However, if I use that SAME query to try
and fill the column during insert, I still get only a date value.

I've even tried some date calculations like Now() + 0 but still get
ONLY a date, no time info.

Recursive Function In Mysql ?
My table
--------

id -- value -- ref_id
----------------------
1--rqwer-0
2--23ew-1
3--23ewe-2
4--123ff-1
5--de123-4

Now i want to take the values like id and refer has to be same and one more condition ,
i need the output as

1,2,4,5

How can i get ?

ENCRYPT Function In Mysql
My password column is encrypted using ENCRYPT() function. When I execute the following query in phpmyadmin, it is showing 0 records. Because everytime, encrypt function is executed, it is returning new value. But my squirrelmail application is decrypting the password correctly.

$query="SELECT * FROM users WHERE email = 'xxx@xxx.com' and password=ENCRYPT('smart')";

how to check whether the user entered password is correct as in mysql?

Mysql Stored Function ...
I'm new to creating stored procedure - can someone tell me where i've gone wrong here - the below code refuses to execute. Essentially, all i want do it something like;

INSERT INTO sometable(a,b,c) VALUE(12,'12',itypes('information'));

if the category 'information' doesn't exist in the event_types table, it's create and the id return - else just returns the id

CREATE FUNCTION .`itypes` ( _tname VARCHAR(45))
RETURNS INT
BEGIN
DECLARE tmp INT;
SELECT id INTO tmp FROM event_types WHERE tname LIKE _tname;
IF (id IS NULL) THEN INSERT INTO event_types ( tname ) VALUES ( _tname );
END IF;
SELECT id INTO tmp FROM event_types WHERE tname LIKE _tname;
RETURN tmp
END

Init_cap() Function In MYSQL
Is there any string function available to get the First Character alone with upper case in MYSQL5.0

For Ex:
remma.

Output:
Remma

Push/pop Function For MySQL
if MySQL has a push/pop function? I'm creating a table for news items, with three rows - one for the date, one for the title, and one for the URL. What I want to do is limit the number of news items held in the table to 10. For example, if there are 10 entries in the table already (date/title/url sets) and I add another item, I want to put it at the top of the table and pop the bottom value off the table.

I know I can do this by exporting the table to an array in PHP, then messing with it there and writing it back to the table, but this would take a lot of MySQL queries I think.

Is there an easy solution to this problem I'm not seeing?

MySQL IF() Function Problem
I want to select 2 dates from a database (fields called start_date and end_date). However, I want to in some way make the returned value of end_date equal to that of start_date if end_date is null.
I've tried using the if() function as follows:

$result = mysql_query("select start_date, if(end_date is null, start_date) from events");

Where events is my table name. This doesn't work. I've tried it with quotes around start_date in the if but that just returns the word start_date.


Variable Function In MySQL
Is there a way to use a 'variable function' in MySQL, e.g.Code:

SELECT @hash:= value FROM prefs WHERE key = 'hash_method';
-- @hash = SHA1
SELECT @hash('test string');

So the variable @hash stores the current hash method in the `prefs` table.
This method does not work, is there a way to do this?

An IMPLODE() Type Function For MySQL??
I'm looking to pull data (just one column) from mysql which is over multiple rows.

e.g.

apple
banana
orange
pear

Now, I actually want the data in this format in just one row:

"apple,banana,orange,pear"

Does mysql offer some sort of "SELECT IMPLODE(',', my_column) AS foo" type of thing? I know I can do it with PHP but this a part of a much larger query so I'm trying to keep this all in a single query.

Is There An 'Is Negative' MySQL Query Function?
I have a query, as shown below. Its a grouped query and I have a column called quantity in a products table. If the value in this table drops below zero I want the query to take zero as the quantity. Is this possible?.....

Mysql 4.x Week Function Fails
I think there is a bug in mysql 4:

select week("2003-08-15") from po.orders limit 1;

produces: 32

after:

set session default_week_format=3;

it produces: 33 (which is correct in the Netherlands)

The documentation states:

set global default_week_format=3; (As root!)

This command should set the default_week_format. This command fails:

mysql> set global default_week_format=3;
Query OK, 0 rows affected (0.00 sec)

mysql> select week('2003-08-14');
+--------------------+
| week('2003-08-14') |
+--------------------+
| 32 |
+--------------------+
1 row in set (0.00 sec)

This result is incorrect. It should be 33.

If I use:

mysql> set session default_week_format=3;
Query OK, 0 rows affected (0.00 sec)

mysql> select week('2003-08-14');
+--------------------+
| week('2003-08-14') |
+--------------------+
| 33 |
+--------------------+
1 row in set (0.00 sec)

This is result is correct...

I use mysql 4.0.14. I also did the same test on mysql 4.1 alpha, which has
the same results.

Harm de Laat
Informatiefabriek
The Netherlands

Is There A Addslashes()-type Function In Mysql Or Sql
I was wondering if there was a way to automatically add escapes to a string in either SQL standard or MySQL, such as addslashes() in PHP. I'm looking for something like the quote() function but without the need for quoted strings?

PHP/MySQL, No Longer Connecting To Db When In Function
I'm trying to clean up the code so that design changes will be easier. So, I've put the

$connection = mysql_connect($hostName,$username,$password) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db($databaseName, $connection);

etcetera... with output commands in global functions().

But for some reason (why?), it no longer "reads" the original include with all the database variables. Is there a way to help these new functions "read" the previously set variables?

Is There A Addslashes()-type Function In Mysql Or Sql
I was wondering if there was a way to automatically add escapes to a
string in either SQL standard or MySQL, such as addslashes() in PHP. I'm
looking for something like the quote() function but without the need for
quoted strings?

Escaping Charater Function In Mysql
Is there any escaping character function available in mysql. I am creating report using stored procedure by concat various data's. Within the data's some special character found like (" ' ) i need to escape those character from mysql function.

UserDefined Function :MySQL Restarts
I am attempting to create a Function by means of using the following query create function xxxx returns integer so name yyyyy.so . The Function gets created Properly at the first time. But when i attempt to Create the same function again after dropping it the MySQL Server gets restarted and the function is not getting created. I don't know what will be problem.....Could any one give me a solution or  hint where will be the problem.

Problem Logging In Using Mysql Password Function
I have a test server on a windows box with php 4.3.10 and mysql 4.1.10-nt. I built a login app and It works well using the mysql password('') function. Unfortunately I ported the app to a freebsd box running php 4.3.10 and mysql 4.1.12. The passwords come out differently and I don't understand why.

For instance, the password test looks like "378b243e220ca493" on the windows box and the freebsd database entry look like "*94BDCEBE19083CE". As you can see, very different. And this causes a lockout from logging in.

Instead of being routed to a "safe" login page, the error for a wrong username/password combo shows up.

Why Does GROUP_CONCAT() Function Fail In MySQL 4.1.12 In This Query?
Here are my results using MySQL 4.1.12 and I honestly don't think this is right, but can't figure out what to do:

Stored Function Stops MySQL Service
I'm using MySQL 5, Windows XP Home SP2 (ack, but only for the near future)... I have what appears to be a simple and straightforward need- execute a function for a column in each row of a result set: Code:

SQLColumns Function And MySQL ODBC Driver
The MySQL ODBC driver returns an error when calling SQLColumns function with an empty Table parameter:

[MySQL][ODBC 3.51 Driver][mysqld-4.0.18-nt]Can't use wildcards in table name

Is there a newer version which supports wildcards in table name? Actually I don't really need wildcards, but I need to get the list of all columns in all tables.

How Can I Execute A PreparedStatement Inside Mysql Function
How can i execute a preparedStatement inside mysql function?

Wot Is The Function To Count The Records In Selelct Statmtn In MySQL
wot is the function to count the records in Selelct statement in MySQL?

Just like
PHP

mysql_num_rows($result);

in php.

Mysql Appears To Have Changed The 'password' Function Hashing
I've been using Mysql for about 6 months but in fairly basic fashion. For a cms I have built, I have a users table, with userName and password fields. An admin can create new users, and the password is hashed using the PASSWORD() function.
SQL: "INSERT INTO users VALUES ('', '$userName', PASSWORD('$userPwd'))

Straight out of the book basically. Now the problem is that all this has been functioning fine on a couple of sites on external servers, plus a couple of local machines.

Until today, on my main dev machine - yesterday a user could log in, today they couldn't. As far as I can tell, nothing has changed. But when I look at the hashes in the database table, they are 41 chrs, and when I add a new user, the hash is 16 characters.

I know that at some stage the hash length was increased between mysql versions, but I have not updated the install (I run XAMPP for Windows 1.4.14, which appears to install php 5.0.4 and mysql 4.1.12)

Is there any other reason that the hash length could change to 16? I'd like to know why this happened so that I'm prepared if it happens on the server (where it is not so easy to hack in and add new users that can log in again)

Union Without Union
I am running version 3.23 of MySQL. It does not allow UNION statements.
How can I pull this off without a union. I hear it can be done with a LEFT JOIN

SELECT `id`,`name`,`date_of_event` FROM `events` ORDER BY `date_of_event` ASC
UNION
SELECT `id`,`name`,`date_of_event` FROM `upcoming` ORDER BY `date_of_event` ASC

Using UNION
(SELECT id,name FROM table1)
UNION
(SELECT id,name FROM table2)
UNION
(SELECT id,name FROM table3)
order by name limit 1;

this query is works. I can get the fields values. But i cant get the owner table that i read. for example i get this values 3,george

(which table has this record???)

UNION On Ver. 3.23
Are there any alternatives to UNION? Running mySQL ver. 3.23
Upgrade is not an option!

My query looks like this:

(SELECT sum(t.timer) AS sumtimer FROM emply
WHERE t.tmnr = 1 AND t.tjobnr = 69 AND
t.tfaktim = 2 AND t.tdato BETWEEN '05/7/4' AND '06/2/1')

UNION (

SELECT sum(f2.timer) AS fakbare FROM timer f2 WHERE
f2.tjobnr = 69 AND f2.tmnr = 1 AND f2.tfaktim = 1 AND f2.tdato BETWEEN '05/7/4' AND '06/2/1')

Union
I have X number of identical (structure-wise) tables, with  one generated for every day.  There is a reporting tool which requires that certain data be extracted for a given time period, which can span any number of these databases.  My idea is to use a dynamically generated UNION ALL statement which simply joins identical selects for each table.  e.g.

CODESELECT id, blah, bleh FROM t1
UNION ALL
SELECT id, blah, bleh FROM t2
....

UNION HELP
I have written a PHP search App and I want it to search between like 8 or so tables in one Database.

Here is my union mysql code:

$query = "(select * from arcamax where promo_code like "%$trimmed%" or start_end_date like "%$trimmed%" or mailedlist like "%$trimmed%" or description like "%$trimmed%" or orders like "%$trimmed%" or totalrevenue like "%$trimmed%") union (select * from drudge where promo_code like "%$trimmed%" or start_end_date like "%$trimmed%" or mailedlist like "%$trimmed%" or description like "%$trimmed%" or orders like "%$trimmed%" or totalrevenue like "%$trimmed%") union (select * from google where promo_code like "%$trimmed%" or start_end_date like "%$trimmed%" or mailedlist like "%$trimmed%" or description like "%$trimmed%" or orders like "%$trimmed%" or totalrevenue like "%$trimmed%") union (select * from human_events where promo_code like "%$trimmed%" or start_end_date like "%$trimmed%" or mailedlist like "%$trimmed%" or description like "%$trimmed%" or orders like "%$trimmed%" or totalrevenue like "%$trimmed%") union (select * from newsmax where promo_code like "%$trimmed%" or start_end_date like "%$trimmed%" or mailedlist like "%$trimmed%" or description like "%$trimmed%" or orders like "%$trimmed%" or totalrevenue like "%$trimmed%") union (select * from street where promo_code like "%$trimmed%" or start_end_date like "%$trimmed%" or mailedlist like "%$trimmed%" or description like "%$trimmed%" or orders like "%$trimmed%" or totalrevenue like "%$trimmed%") union (select * from townhall where promo_code like "%$trimmed%" or start_end_date like "%$trimmed%" or mailedlist like "%$trimmed%" or description like "%$trimmed%" or orders like "%$trimmed%" or totalrevenue like "%$trimmed%") union (select * from weatherbug where promo_code like "%$trimmed%" or start_end_date like "%$trimmed%" or mailedlist like "%$trimmed%" or description like "%$trimmed%" or orders like "%$trimmed%" or totalrevenue like "%$trimmed%") union (select * from worldnet where promo_code like "%$trimmed%" or start_end_date like "%$trimmed%" or mailedlist like "%$trimmed%" or description like "%$trimmed%" or orders like "%$trimmed%" or totalrevenue like "%$trimmed%") order by mailedlist";

Now if I run a search with the above code it does not work...but if I limit the number of tables to two it works fine.

How To Do A Union
I have a query I need to do that combines two queries The structure for the first table is

Application_Period
CampusID
GroupID
IndivID

This table uses the lookup table Time to get the groupID and IndivID.

TimeID
Application_Period

TimeID matches GroupID and IndivID. How can I combine the two tables into one without the UNION operator?


Copyright © 2005-08 www.BigResource.com, All rights reserved