Selecting from 2 tables in a single query - php

I have a table that I'm querying for value 43 in the second field and I return the value of the third field
SELECT t1_field3 FROM table1 WHERE t1_field2=43
this returns 19, 39,73
t1_id t1_field2 t1_field3
----- --------- ---------
1 43 19////
2 43 39////
3 43 73////
4 73 43
5 13 40
Then I separately query a second table for additional information
SELECT * FROM table2 WHERE t2_id=t1_field3
t2_id t2_field2 t2_field3
----- --------- ---------
19 value19.2 value19.3
39 value39.2 value39.3
73 value73.2 value73.3
Is there a way I could combine both table1 and table2 in the same query?

You're describing a JOIN. In this case you don't need to explicitly use the JOIN keyword though, you can just do this:
SELECT table1.t1_field3, table2.* FROM table1, table2 WHERE table1.t1_field2=43 AND table2.t2_id = table1.t1_field3
It would probably be helpful to learn about the different types of joins at some point; Coding Horror has a good post about it

select * from table2
where t2_id in (select t1_field3 from table1 where t1_field2=43 )

There's a way to express the join directly as well like so:
select table1.t1_field3, table2.*
from table1
join table2 on table1.t1_field3 = table2.t2_id
where table1.t1_field2 = 43;

Related

How to sum a column with a foreign value selecting by id

I've a table syntax like this,
- table1
id foreign_table_id
-----------------------
1 2
2 2
3 2
4 1
5 1
6 1
The other table is,
- table2
id value
------------
1 20
2 10
I want to get a summation of table1.foreign_table_id where the data will retrieve from table2.
Like this example should produce the result 90
Please provide any solution.
Use below query.
Select sum(table2.value) as total FROM table1 LEFT JOIN table2 ON table1.foreign_table_id = table2.id
It will give u below result.
Output::
total
-----
90
SELECT SUM(table2.value) AS summation FROM table1 LEFT JOIN table2
ON table2.id = table1.foreign_table_id

MySQL: Select several rows based on several keys from two different tables

I have these two tables - user_schedules and user_schedule_meta, shown below:
------------------------------------
| id | scheduler_id | status |
------------------------------------
1 3 pending
2 5 active
3 6 active
and
----------------------------------------------
| id | user_schedule_id | meta_key |meta_value
----------------------------------------------
1 3 course-id 135
2 3 session-id 15
3 3 schedule-id 120
I want to write a query to enable me select, for example, from both tables where EVERYONE of the below 5 conditions are met:
user_schedule_id = 3
scheduler_id = 6
session_id = 15
course-id = 135
schedule-id = 120
This is what I have so far, but it is not working:
SELECT user_schedule_meta.`id` FROM user_schedule_meta, user_schedules
WHERE user_schedules.`scheduler_id` = 6
AND user_schedules.id = user_schedule_meta.`user_schedule_id`
AND (
(user_schedule_meta.`meta_key` = 'course-id' AND user_schedule_meta.`meta_value` = 135)
OR (user_schedule_meta.`meta_key` = 'session-id' AND user_schedule_meta.`meta_value` = 15)
OR (user_schedule_meta.`meta_key` = 'daily-schedule-id' AND user_schedule_meta.`meta_value` = 120)
)
GROUP BY user_schedule_meta.`id`
Any suggestions what I am not doing right?
This is a typical key-value store lookup problem. These are trickier than they look in SQL, in that they require multiple JOIN operations.
You need a virtual table with one row per user_schedules.id value, then you can filter it. So
SELECT u.id, u.scheduler_id
FROM user_schedules u
JOIN user_schedule_meta a ON u.id=a.user_schedule_id AND a.meta_key='course-id'
JOIN user_schedule_meta b ON u.id=b.user_schedule_id AND b.meta_key='session-id'
JOIN user_schedule_meta c ON u.id=c.user_schedule_id AND c.meta_key='daily-schedule-id'
WHERE a.meta_value = 135 -- value associated with course-id
AND b.meta_value=15 -- value associated with session-id
AND c.meta_value=120 -- value associated with daily-schedule-id
Notice also that you can list your table with associated attributes like this. This trick of joining the key/value table multiple times is a kind of pivot operation. I use LEFT JOIN because it will allow the result set to show rows where an attribute is missing.
SELECT u.id, u.scheduler_id, u.status,
a.meta_value AS course_id,
b.meta_value AS session_id,
c.meta_value AS daily_schedule_id
FROM user_schedules u
LEFT JOIN user_schedule_meta a ON u.id=a.user_schedule_id AND a.meta_key='course-id'
LEFT JOIN user_schedule_meta b ON u.id=b.user_schedule_id AND b.meta_key='session-id'
LEFT JOIN user_schedule_meta c ON u.id=c.user_schedule_id AND c.meta_key='daily-schedule-id'
try this is code
select * from user_schedule_meta where user_schedule_id=3 and
(meta_key='session-id' AND meta_value=15
or meta_key='daily-schedule-id' AND meta_value=120
or meta_key='course-id' AND meta_value=135
)

Return all the rows even the joined table has empty results

In my table 1 I have something like this
name | age
George 42
Bob 30
Ken 23
In my table 2, I have something like this, this is where i store votes for each person.
name | votes |
George 1
Ken 1
George 1
George 1
Ken 1
My goal is to combine the 2 tables, and return all the rows in table 1 even it doesn't exist in table 2.
Desire results:
name | age | total_votes
George 42 3
Bob 30 0
Ken 23 2
But instead I get:
name | age | total_votes
George 42 3
Ken 23 2
I have tried something like this
SELECT `table_1`.*, coalesce(COUNT(`table_2`.votes), 0) AS total_votes
FROM `table_1`
LEFT JOIN `table_2`
ON `table_1`.name = `table_2`.name
You can do one of these:
1) Use Right Join instead of current Left Join.
Or
2) Exchange table1 and table2 places in your join expression, like:
FROM table_2
LEFT JOIN table_1
Try this. This works in MS Access , I think this will work on your's too just convert the query to SQL:
SELECT Table1.name, First(Table1.age) AS age, Count(Table2.Votes) AS totalVotes
FROM Table1 LEFT JOIN Table2 ON Table1.name = Table2.name
GROUP BY Table1.name;
Left Join table1 to table2 so that all entry from table1 , even if its is corresponding data is null, will be included. GROUP BY your query by name so that votes will be counted by name .

Retrieving data from two tables at once with one table referencing the other

I have two table as follows:
TABLE 1:
ID Code Detail
1 45 Yes
2 46 No
AND TABLE 2:
Code Detail1 Detail2
45 No 23
22 Yes 34
Is it possible to select all FROM TABLE 2 where detail = YES AND where TABLE 1 contains Code and says Yes?
i.e. query result should be:
Code Detail Detail
45 No 23
22 Yes 34
Thanks.
SQL Fiddle: http://sqlfiddle.com/#!2/6f583/9
SELECT
Table2.Code,
Table2.Detail1,
Table2.Detail2
FROM
Table1,
Table2
WHERE
Table1.Detail = "Yes" OR
(Table2.Detail1 = "Yes" AND
Table1.Code = Table2.Code)
Although, my above query seems to give you the correct resultset for the example, I think that the following query better satisfies your criteria:
SQL Fiddle: http://sqlfiddle.com/#!2/6f583/12
SELECT
Table2.Code,
Table2.Detail1,
Table2.Detail2
FROM
Table1,
Table2
WHERE
(Table1.Code = Table2.Code AND
Table1.Detail = "Yes") OR
Table2.Detail1 = "Yes"
GROUP BY
Table1.ID
I don't follow your question very well but this will create the table of results that you want.
select table2.* from table2 left join table1 on table2.code = table1.code where table2.Detail1 = 'Yes' or table1.Detail = 'Yes'

Joining Two SQL Tables in a query

I have two tables (with the same schema)
Table1
id title D0 D1 D2 D3
------------------------------------
1 Title1 0.12 3.23 4.90 -0.12
1 Title1 0.22 0.32 -4.90 0.12
1 Title1 0.13 1.24 3.50 -0.22
...
1 TitleN 1.22 2.33 3.90 -1.56
and
Table2
id title D0 D1 D2 D3
------------------------------------
1 Title1 1.42 -0.93 -2.99 3.22
1 Title1 0.52 3.32 -4.90 0.54
1 Title1 2.13 1.14 3.50 -0.22
...
1 TitleN 3.42 4.37 3.90 -1.26
I am trying to figure out how to do a query like can do this math:
SELECT title FROM Table2 WHERE (Table1_Row1_D0*Table2_Row1_D0)+(Table1_Row1_D1*Table2_Row1_D1)+(Table1_Row1_D2*Table2_Row1_D2) < 0.5;
However, I would like the query to iterate through the rows of Table1 and perform the SELECT against the entire Table2. Basically, I want to select the titles from Table2 where the calculation inequality is met against ALL the row combination of Table1 and Table 2.
Is this possible???
Not sure it matters, but I am using Postgre.
Basically, I want to select the titles
from Table2 where the calculation
inequality is met against ALL the row
combination of Table1 and Table 2.
For that you will want the reverse condition, where there does NOT exist an equality in Table1 for that Table2 row.
SELECT distinct title
FROM Table2
WHERE NOT EXISTS (
SELECT *
FROM Table1
WHERE (Table1.D0*Table2.D0)+(Table1.D1*Table2.D1)
+(Table1.D2*Table2.D2) >= 0.5
)
You'll want a CROSS JOIN
SELECT Table2.title
FROM Table2
CROSS JOIN Table1
WHERE (Table1.D0*Table2.D0)+(Table1.D1*Table2.D1)+(Table1.D2*Table2.D2) < 0.5;
You should be using a union. The only caveat is your returning fields from your selects must match
(SELECT * FROM Table1 WHERE conditions) UNION (SELECT * FROM Table2 WHERE conditions)
Do your checks on the script side as long as your not pulling up too much data. You also have the option to add sub selects to the where condition to limit both sides of this union query.

Categories