sql where clause for each row of a subquery - php

I have 2 tables in MSSql
table_a:
data|id_1|timeInIntForm
------------------------------
data1 | 1/22323/3 | 1433721600
data2 | 1/22323/3 | 1433721660
and I have another table
table_b
data|(string list of ids)|startTimeinIntform|EndTimeInIntForm
--------------------------------------------------------------
dataA| (a_1223233_z a_1223233_x) | 1433721601 | 1433721659
datab| (a_1223233_z a_1223233_x) | 1433721602 | 1433721645
I want to do a
select * from table_a where id_1 = 'someId'
and timeInIntForm between 'time1' and 'time2'
and not between TableB_row1[startTimeinIntform] and TableB_row1[EndTimeInIntForm]'
.
.
.
and not between TableB_rowN[startTimeinIntform] and TableB_rowN[EndTimeInIntForm]'
The ids do not match but can be correlated on a different table that has
table_c:
data|id_1|id_2
-----------------------------
data| 1/22323/3 | a_1223233_z
data| 1/22323/4 | a_1223233_x
my initial thought is in PHP to get the rows from table_b and then as I select from table_a build the not between clause from each row in table_b. But I would LOVE to do this in one select statement if possible. Any ideas?
I edited it to have some "sample" data I was asked for. I am stumped on how to join these .. I admit freely I am not a pro or even poor at joins but I am really stumped on how to join table b to a and c

Well I am not sure exactly without the tables but I think this would do it.
Select a.*
From table_a a
join table_c c
on a.ID_1 = c.ID_1
join table_b b
on c.ID_2 = b.ID_2
Where a.ID_1 = 'someid'
and a.timeinintform not between 'time1' and 'time2'
and a.timeinintform not between b.starttimeininform and b.endtimeinintform

Related

How to get 3 Users on one ROW using SQL ? (MySQL)

I have two arrays like this,
First Table (infos):
------------------------------
| ID | User1 | User2 | User3 |
------------------------------
| 1 | 20 | 30 | 12 |
------------------------------
Second Table (Users):
---------------------
| ID | Name | Email |
---------------------
| 12 | Test | Test# |
---------------------
| 20 | Bla | Test# |
---------------------
| 30 | Bate | Test# |
---------------------
I want to get the information of users on one row from the IDs on the first table.
I try by getting The row from the first table and fetching on users, but I want to optimize the function with just one Query.
SELECT * FROM infos;
SELECT * FROM Infos i,Users u WHERE u.ID = u.User1 (or 2 ...)
Is there any solution ?
You could use joining the table users 3 times, one for each userid you want show the related name (or other values):
select a.id
, a.user1
, b.Name as user1name
, a.user2
, c.name as user2name
, a.user3
, d.name as user3name
from infos a
inner join Users b on a.user1 = b.id
inner join Users c on a.user1 = c.id
inner join Users d on a.user1 = d.id
And just as suggested, you should not use old implicit join syntax based on comma-separated table names and where clause, you should use (since 1992) explicit joins. This syntax performs the same query, but is more clear.
This is a design error. Use a N:N relation (an additional table) to allow any number of users for the first table. With the relation, other queries will be easier.
A relation table looks like this:
create table relation
(
table1_id int unsigned not NULL,
table2_id int unsigned not NULL,
primary key(table1_id,table2_id)
);
A typical query (and I dislike a.* generally):
select a.*, b.*
from table1 a, table2 b, relation r
where r.table1_id = a.id
&& r.table2_id = b.id

Selecting one row value based on anothers row value

Hi, guys
Can't find answer in other topics, so asking here.
I have a Table in database
Table
------------------------------
id | name | last_name | created_by_id |
1 | Bilbo ..| Baggins.....| 0 .................... |
2 | Frodo . | Baggins.....| 1 ................... |
Is there any way i can get 1st row name value by using 2nd row created_by_id ?
I need to get sentence Frodo Baggins was created by Bilbo Baggins.
Can't find the right sql sentence
You need self join :
select t.*, t1.name, t1.last_name
from table t inner join
table t1
on t1.id = t.created_by_id
where t.id = 2;
you can just use a join
select *,<your string stuff here>
from <table> as a
inner join <table> as b
on a.id = b.created_by_id

SQL inner join of multiple tables and search through database

I'm making a search function in PHP and I have three tables that I wish to join to a single one; the three tables looks as follow:
band
ID | bands
---+----------
1 | Muse
2 | Coldplay
3 | etc.
release
ID | releases
---+----------
1 | Showbiz
2 | Origin of Symmentry
3 | etc.
track
ID | tracks
---+-----------
1 | Sunburn
2 | Muscle Museum
3 | etc.
I want these tables to be put into this:
discografic
ID | band_id | release_id | track_id
---+----------+-------------+---------
1 | 1 | 1 | 1
2 | 1 | 1 | 2
3 | etc.
So that the table with the SQL code looks like this:
discografic
ID | bands | releases | tracks
---+----------+-------------+---------
1 | Muse | Showbiz | Sunburn
2 | Muse | Showbiz | Muscle Museum
3 | etc.
I want to INNER JOIN these tables. I joined one but I can't really figure out how the get the last joined as well.
SELECT *
FROM band
INNER JOIN discografic
ON band.id = discografic.band_id
This should probably have its own question; I also want to be able to search this database, but only have the result show up once, and also reference to the band every time. For example, if I search "Showbiz" it will give me "Muse", and only show it once.
Note: This is for testing purposes only, security is none of my concerns.
Try with this query:
select d.id,b.bands,r.releases,t.tracks from discografic as d INNER JOIN band as b on
d.band_id=b.id INNER JOIN release as r on d.release_id=r.id INNER JOIN track as t on
d.track_id=t.id GROUP BY d.id
Try This query
Select a.ID,b.bands,c.releases,d.tracks from discografic as a
inner join band as b on a.band_id = b.ID
inner join release as c on a.release_id = c.ID
inner join track as d on a.track_id = d.ID
where b.bands = 'Muse'
Use this query to insert the data like you wanted:
Insert into discograpy
(id,bands,releases,tracks)
SELECT band.ID,bands,releases,tracks
FROM band
INNER JOIN releases
ON band.id = releases.id
inner join track
on band.id = track.id
Use this query to show you only one band:
Declare #releases varchar(50)
Set #releases = 'showbiz'
SElect distinct bands from discograpy where releases = #releases
Here any variable can be passed or set in place of showbiz. This is an example

Selecting unique id in mysql

Below is the replicas of the tables I have created. My goal is to simply pick the unique id_num from the First Table which is not found on the Second Table.
I have tried doing the code below but somehow, I kept getting empty results
SELECT `first_table`.name FROM `first_table`
INNER JOIN `second_table`
ON `first_table`.id_num = `second_table`.id_num
WHERE `first_table`.name = `second_table`.name
First Table:
id_num | name
301 | Bobby
123 | George
25 | Vicky
Second Table:
id_num | name
301 | Bobby
435 | George
25 | Vicky
My desire result I am looking for:
id_num | name
435 | George
LEFT JOIN should work here.
SELECT `first_table`.name FROM `first_table`
LEFT JOIN `second_table`
ON `first_table`.id_num = `second_table`.id_num
WHERE `second_table`.id_num is NULL
See also this useful infographic
try this using NOT IN
select `id_num` , name from `table2` where name not in (
SELECT t1.name FROM `table1` t1
INNER JOIN `table2` t2
ON t1.id_num = t2.id_num )
DEMO HERE

MySQL join same table twice on different columns without a clash

This is the query:
SELECT * FROM property_table AS property
INNER JOIN property_classification AS classifications
ON property.classification_id = classifications.id
INNER JOIN property_classification AS classonrequest
ON property.classonrequest_id = classonrequest.id
WHERE property.id=5000 LIMIT 1;
Notice that I'm using the same table property_classification on two fields property.classification_id and property.classonrequest_id.
The structure of property_classification is something like:
id | a1 | a2 | a3 | ... | d1 | d2
When I execute the query above in MySQL Query Browser, I get something like this:
id | other 'property' fields | id | a1 | a2 | a3 | ... | id | a1 | a2 | a3 | ...
But in my PHP script I am returning associated arrays, and all duplicate field names are overwritten.
What I want is the query to return the two joined tables under the name of their table i.e.:
classifications.id | classifications.a1 | classifications.a2 | classifications.a3
and
classonrequest.id | classonrequest.a1 | classonrequest.a2 | classonrequest.a3
How do I do that?
You need to use table aliases and rename the columns:
SELECT classifications.id as cid,
classifications.a1 as c_a1,
. . .
classificaitions.d2 as c_d2
classonrequest.a1 as cr_a1,
. . .
FROM property_table AS property
INNER JOIN property_classification AS classifications
ON property.classification_id = classifications.id
INNER JOIN property_classification AS classonrequest
ON property.classonrequest_id = classonrequest.id
WHERE property.id=5000
LIMIT 1;
To make your job easier, you can run a query like:
select concat('c_', column_name, ', ')
from information_schema.columns
where table_name = 'classification';
This will list all the column names for the first group (and you can repeat for the second group using a different prefix.
SELECT property.*, classifications.*, classonrequest.* FROM property_table AS property
INNER JOIN property_classification AS classifications
ON property.classification_id = classifications.id
INNER JOIN property_classification AS classonrequest
ON property.classonrequest_id = classonrequest.id
WHERE property.id=5000 LIMIT 1;
But you still won't get the specific table names.

Categories