DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   MySQL (http://www.daniweb.com/forums/forum126.html)
-   -   Max date to max date (http://www.daniweb.com/forums/thread124521.html)

stonyheng May 16th, 2008 5:01 am
Max date to max date
 
Hi all,

I wonder if it is possible that someone can drop me a code or something on this particular problem which I am currently dealing with.

in_date out_date
18/03 30/04
20/04

So how am i supposed to pair up 30/04 and 20/04 as 1 pair and show it up in the table? Many thanx in advance

Daedal May 20th, 2008 4:59 pm
Re: Max date to max date
 
Here is an example... could you try to be more specific?

This will select records with an in_date of '%-03-18' or out_date of '%-04-20':

SELECT * 
FROM table
WHERE in_date LIKE '%-03-18'
OR out_date like '%-04-20'

jemajoign May 21st, 2008 2:49 pm
Re: Max date to max date
 
If by 'pair up' you mean that you want them to appear in the same row, I think the approach would be to join the table to itself.

So if you had a table like:
id        description        linkid
1        item 1                3
2        item 2                1
3        item 3                2

And you want a result like:
leftdescription        rightdescription
item 2                item 1
item 3                item 2
item 1                item 3

A query like this should do it:

create table if not exists link2myself(id int, description varchar(6), linkid int);
insert into link2myself(id,description, linkid)
select 1, 'item 1', 3 union
select 2, 'item 2', 1 union
select 3,'item 3', 2;

select me.description leftdescription, otherme.description rightdescription
from link2myself me
join link2myself otherme on me.linkid=otherme.id;

drop table link2myself;

Traicey May 26th, 2008 11:50 am
Re: Max date to max date
 
Lets say you really dont want to create a table and you dont want to join tables but concatenating two records from one table instead...........

I think you need to be more specific coz what u said above is completely unclear

jemajoign May 26th, 2008 1:10 pm
Re: Max date to max date
 
Just as a note, the 'CREATE TABLE' is used to create a sample table with some sample data. It has no relation to the solution. In practical usage, you would use the table you already have.

Also, if you want to string concatenate some rows you can use the aggregate function group_concat(). But if you want them to appear as separate columns, use join.


All times are GMT -4. The time now is 11:24 pm.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC