beridius
asked on
mysql join
I having some problem in joining two tables together one has the number in eg 1 2 3 4
the other table has id then some text eg id 1 text hello, id=2 text goodbye
the other table has id then some text eg id 1 text hello, id=2 text goodbye
SELECT *
JOIN `ClientSource` ON `sourceID` = `ClientSource` ;
that what I tried with no luck
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
beridius I think your problem is related to convertion..
see this code:
-------------------------- ---------
declare @ClientSource table (Int_column int)
declare @ClientDestination table (string_column varchar(50))
insert into @ClientSource select 1 union select 2 union select 3
insert into @ClientDestination select '1 text hello' union select '2 text goodbye'
SELECT *
FROM @ClientSource CS
JOIN @ClientDestination CD ON CS.Int_column = CD.string_column
-------------------------- ---------
It will give an error like
Conversion failed when converting the varchar value '1 text hello' to data type int.
You need to cast the datatype to avoid the error;
SELECT *
FROM @ClientSource CS
JOIN @ClientDestination CD ON cast(CS.Int_column as varchar(50))= CD.string_column
This will solve the problem
see this code:
--------------------------
declare @ClientSource table (Int_column int)
declare @ClientDestination table (string_column varchar(50))
insert into @ClientSource select 1 union select 2 union select 3
insert into @ClientDestination select '1 text hello' union select '2 text goodbye'
SELECT *
FROM @ClientSource CS
JOIN @ClientDestination CD ON CS.Int_column = CD.string_column
--------------------------
It will give an error like
Conversion failed when converting the varchar value '1 text hello' to data type int.
You need to cast the datatype to avoid the error;
SELECT *
FROM @ClientSource CS
JOIN @ClientDestination CD ON cast(CS.Int_column as varchar(50))= CD.string_column
This will solve the problem
which database system are you using ... they all have there own syntax styles...
ASKER
phpmyadmin (mysql)
so remove the ms sql server and database miscellaneous zones...
you query is mysql specific...
can you show us some example data for the two tables from your description of the problem
it isn't clear how the data is represented within the tables..
we also need to know the datatypes of the columns
you query is mysql specific...
can you show us some example data for the two tables from your description of the problem
it isn't clear how the data is represented within the tables..
we also need to know the datatypes of the columns
ASKER
Table structure for table tbl_source
Column Type Null Default
sourceID int(11) No
sourceName varchar(50) Yes NULL
Dumping data for table tbl_source
sourceID sourceName
DATA
1 Website Browsing
2 Mailshot
3 Passing Or Local
4 Recommended
Table structure for table tbl_client
Column Type Null Default
clientID int(11) No
Client_level int(10) No 1
clientTitle varchar(50) Yes NULL
ClientFirstname varchar(50) Yes NULL
ClientSurname varchar(50) Yes NULL
ClientAddress1 varchar(50) Yes NULL
ClientAddress2 varchar(50) Yes NULL
ClientAddress3 varchar(50) Yes NULL
ClientAddress4 varchar(50) Yes NULL
ClientAddress5 varchar(50) Yes NULL
ClientPostcode varchar(50) Yes NULL
ClientCountry varchar(255) Yes NULL
clientDeliveryTitle varchar(50) Yes NULL
ClientDeliveryFirstname varchar(50) Yes NULL
ClientDeliverySurname varchar(50) Yes NULL
ClientDeliveryAddress1 varchar(50) Yes NULL
ClientDeliveryAddress2 varchar(50) Yes NULL
ClientDeliveryAddress3 varchar(50) Yes NULL
ClientDeliveryAddress4 varchar(50) Yes NULL
ClientDeliveryAddress5 varchar(50) Yes NULL
ClientDeliveryPostcode varchar(50) Yes NULL
ClientDeliveryCountry varchar(255) Yes NULL
ClientContact varchar(255) Yes 0
ClientSource int(11) Yes 0
ClientSource2 varchar(100) Yes NULL
ClientPhoneHome varchar(15) Yes NULL
ClientPhoneWork varchar(15) Yes NULL
ClientPhoneMobile varchar(15) Yes NULL
ClientPhoneMobile2 varchar(15) Yes NULL
ClientFaxHome varchar(15) Yes NULL
ClientFaxWork varchar(15) Yes NULL
ClientEmail varchar(50) Yes NULL
ClientMailingList tinyint(1) Yes 0
ClientInfoSentNotes longtext Yes NULL
ClientProductNotes longtext Yes NULL
ClientOrderContact int(11) Yes 0
ClientSamplesSent longtext Yes NULL
ClientShowroomVisitDate datetime Yes NULL
Designer varchar(255) Yes NULL
Clientfolderdir varchar(255) Yes NULL
Column Type Null Default
sourceID int(11) No
sourceName varchar(50) Yes NULL
Dumping data for table tbl_source
sourceID sourceName
DATA
1 Website Browsing
2 Mailshot
3 Passing Or Local
4 Recommended
Table structure for table tbl_client
Column Type Null Default
clientID int(11) No
Client_level int(10) No 1
clientTitle varchar(50) Yes NULL
ClientFirstname varchar(50) Yes NULL
ClientSurname varchar(50) Yes NULL
ClientAddress1 varchar(50) Yes NULL
ClientAddress2 varchar(50) Yes NULL
ClientAddress3 varchar(50) Yes NULL
ClientAddress4 varchar(50) Yes NULL
ClientAddress5 varchar(50) Yes NULL
ClientPostcode varchar(50) Yes NULL
ClientCountry varchar(255) Yes NULL
clientDeliveryTitle varchar(50) Yes NULL
ClientDeliveryFirstname varchar(50) Yes NULL
ClientDeliverySurname varchar(50) Yes NULL
ClientDeliveryAddress1 varchar(50) Yes NULL
ClientDeliveryAddress2 varchar(50) Yes NULL
ClientDeliveryAddress3 varchar(50) Yes NULL
ClientDeliveryAddress4 varchar(50) Yes NULL
ClientDeliveryAddress5 varchar(50) Yes NULL
ClientDeliveryPostcode varchar(50) Yes NULL
ClientDeliveryCountry varchar(255) Yes NULL
ClientContact varchar(255) Yes 0
ClientSource int(11) Yes 0
ClientSource2 varchar(100) Yes NULL
ClientPhoneHome varchar(15) Yes NULL
ClientPhoneWork varchar(15) Yes NULL
ClientPhoneMobile varchar(15) Yes NULL
ClientPhoneMobile2 varchar(15) Yes NULL
ClientFaxHome varchar(15) Yes NULL
ClientFaxWork varchar(15) Yes NULL
ClientEmail varchar(50) Yes NULL
ClientMailingList tinyint(1) Yes 0
ClientInfoSentNotes longtext Yes NULL
ClientProductNotes longtext Yes NULL
ClientOrderContact int(11) Yes 0
ClientSamplesSent longtext Yes NULL
ClientShowroomVisitDate datetime Yes NULL
Designer varchar(255) Yes NULL
Clientfolderdir varchar(255) Yes NULL
It looks like tbl_source has sourceID values of 1, 2, 3, and 4. Are there records in tbl_client with clientID values that match (1, 2, 3, or 4)? Are you getting an error when you run the query with the corrected syntax?
ASKER
I just need the syntax to join the tables together (relationship)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The data was in ms access and I have moved it to mysql there were relationship between in ms access
I need away to replicate in mysql
I need away to replicate in mysql
I don't know that relationships have any usefulness in MySQL. I haven't used them in nearly 10 years. Perhaps another expert will comment further.
in MySQL (MyISAM engine), relationships (foreign keys) are irrelevant, because they are not enforced (they are in InnoDB tables...)
however, JOIN does not need or require the foreign keys.
possibly you continue to post more of the sample data?
however, JOIN does not need or require the foreign keys.
possibly you continue to post more of the sample data?
Open in new window