Jump to content
WIK

[SQL] tabele multiple.

Recommended Posts

Bun am 3 tabele ce au urmatoarele coloane:

 

Tabel 1  coloanele sunt: 

id 

Tabel 2 coloanele sunt

id -

identifier (problema este ca aici pe o coloana sunt mai multe rezultate de tip : discord:1321312321, licence2:sadadasdasdasdasdas,license:21312321312312321 eu vreau sa scot doar 

partea cu license: sadasadsa 

ca sa scot asta am folosit concatenare (nimic greu)

SELECT * FROM tabel2 where identifier like '%license:%'

Tabel 3

identifier - timp

 

acum ideea e sa iau id-ul din tabelul 1 ca din tabelul 2 sa iau licenta aferenta id-ului si pe baza licentei gasite in tabelul 2 sa scot valorile din tabelul 3 (cu toate ca pot sa iau licenta bazata pe id direct din tabel 2) am nevoie neaparat ca id-ul sa fie extras din tabel 1 si nu din tabel 2, in tabel 2 sa se faca compararea doar.

Link to comment
Share on other sites

Cu subselect si CONCAT, adica concatenare. Ce ai facut tu cu LIKE nu se numeste concatenare.

SELECT *

FROM tabel1

WHERE CONCAT("license:", id) IN (

SELECT identifier

FROM tabel2

)

 

Asta iti scoate tot din tabel1 a caror id-uri se regasesc in tabel2 in concatenarea "license:xxx". Cu tabelul3 nu prea am inteles care e treaba.

Link to comment
Share on other sites

  • Moderators

Din ce am inteles, structura ta si valorile sunt ceva de genul

 

CREATE TABLE `tabel1` (
  `id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `tabel1` (`id`) VALUES
(1);

CREATE TABLE `tabel2` (
  `id` int(11) NOT NULL,
  `identifier` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `tabel2` (`id`, `identifier`) VALUES
(1, 'license:21312321312312321');

CREATE TABLE `tabel3` (
  `identifier` varchar(100) NOT NULL,
  `timp` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `tabel3` (`identifier`, `timp`) VALUES
('license:21312321312312321', 100);
COMMIT;

 

si vrei o relationare sa-ti aduca identifier din tabelul 3 dupa ID-ul din tabelul 1

 

select tabel3.timp, tabel2.identifier from tabel3
inner join tabel2 on tabel2.identifier=tabel3.identifier
inner join tabel1 on tabel2.id=tabel1.id and tabel1.id=1 # aici pui ID-ul din tabelul 1

 

EDIT: Daca vrei doar ce este dupa : din tabelul 2

 

select tabel3.timp, SUBSTRING_INDEX(tabel2.identifier,":",-1) as "license" from tabel3
inner join tabel2 on tabel2.identifier=tabel3.identifier
inner join tabel1 on tabel2.id=tabel1.id and tabel1.id=1 # aici pui ID-ul din tabelul 1

 

  • Like 1
Link to comment
Share on other sites

31 minutes ago, Dragos said:

Din ce am inteles, structura ta si valorile sunt ceva de genul

 


CREATE TABLE `tabel1` (
  `id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `tabel1` (`id`) VALUES
(1);

CREATE TABLE `tabel2` (
  `id` int(11) NOT NULL,
  `identifier` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `tabel2` (`id`, `identifier`) VALUES
(1, 'license:21312321312312321');

CREATE TABLE `tabel3` (
  `identifier` varchar(100) NOT NULL,
  `timp` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `tabel3` (`identifier`, `timp`) VALUES
('license:21312321312312321', 100);
COMMIT;

 

si vrei o relationare sa-ti aduca identifier din tabelul 3 dupa ID-ul din tabelul 1

 


select tabel3.timp, tabel2.identifier from tabel3
inner join tabel2 on tabel2.identifier=tabel3.identifier
inner join tabel1 on tabel2.id=tabel1.id and tabel1.id=1 # aici pui ID-ul din tabelul 1

 

EDIT: Daca vrei doar ce este dupa : din tabelul 2

 


select tabel3.timp, SUBSTRING_INDEX(tabel2.identifier,":",-1) as "license" from tabel3
inner join tabel2 on tabel2.identifier=tabel3.identifier
inner join tabel1 on tabel2.id=tabel1.id and tabel1.id=1 # aici pui ID-ul din tabelul 1

 

merci mult din nou ! 

Link to comment
Share on other sites

40 minutes ago, Wav3 said:

Cu subselect si CONCAT, adica concatenare. Ce ai facut tu cu LIKE nu se numeste concatenare.

SELECT *

FROM tabel1

WHERE CONCAT("license:", id) IN (

SELECT identifier

FROM tabel2

)

 

Asta iti scoate tot din tabel1 a caror id-uri se regasesc in tabel2 in concatenarea "license:xxx". Cu tabelul3 nu prea am inteles care e treaba.

eu initial am facut cu concatenare da am vazut ca n-am nevoie de asa cv si am modificat putin syntaxa.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...