I have three tables
1>EVENT table columns event_id, time
2>EVENT_TYPE1 table columns ref_event_id
3>EVENT_TYPE2 table columns ref_event_id
now what I want to do is to perform a query so that ref_event_id from both tables EVENT_TYPE1 and EVENT_TYPE2 can be compared to EVENT table ,in single query, and results are arranged in ascending order of time.
You can do something like this:
SELECT a.col1, a.col2
FROM EVENT a
LEFT JOIN
(SELECT col1, col2 FROM EVENT_TYPE1
UNION ALL
SELECT col1, col2 FROM EVENT_TYPE2) b ON b.col1 = a.col1 <== Comparison
This creates a single table to join with ordered as you want
SELECT combined.* FROM (
SELECT * from EVENT_TYPE1
UNION
SELECT * from EVENT_TYPE2) AS combined
I'm a little fuzzy on what you mean by 'Compared to Event table'. You can join the result of this inner select to the Event table on event_id = ref_event_id assuming ref_event_id is a foreign key into the event table.
SELECT * FROM (
SELECT * from EVENT_TYPE1
UNION
SELECT * from EVENT_TYPE2) AS combined
JOIN EVENT ON event_id = combined.ref_event_id
ORDER BY combined.time ASC
Related
I have a main table: Images and votes.
The votes contains 2 fields: Id, image_id
What I wish to do is query the images table and return them ordered by the amount of votes in the vote table.
Currently:
$stmt = $conn->prepare(
'SELECT *
FROM images
LIMIT 10'
);
I understand basic left joins, but cannot think how to order them by count of votes. Just need a starting example.
Thanks
Assuming that each row in your VOTES table means one vote towards the image, this should work: SQL DEMO
SELECT *, COUNT(v.image_id)
FROM images AS i
LEFT JOIN votes AS v ON i.id=v.image_id
GROUP BY i.id
ORDER BY v.image_id desc
drop table if exists t1;
create table t1 (id int);
insert into t1 values (1), (2), (3);
drop table if exists t2;
create table t2 (id int, img_id int);
insert into t2 values (1, 1), (2, 1), (3, 2), (4,1), (5,3), (6,2);
select *
from t1
left join
(select img_id, count(*) count
from t2
group by img_id) c
on img_id=t1.id
order by count
Demo on sqlfiddle
I have the below table (in picture) which is kind of inventory table and shows how many items comes in and how many goes out from stock, and item_id is the foreign key from another table.
I want to select those records that has no out from the stock, in other word i want to select those records which are highlighted in green (in the picture).
Thanks.
Sorry for poor English
The Table
Try this:
Select * from `table` where id in (select id from `table`group by id having sum(out)=0);
for deleting those values use:
delete t1
from `your_table` as t1
join (select item_id from `your_table`group by item_id having sum(item_out)=0) t2 on t1.item_id = t2.item_id
Try this query.
SELECT * FROM 'table_name' where out=0;
You need to join the table to itself: SELECT t.* FROM <your_table> AS t LEFT JOIN <your_table> AS t1 ON t.item_id=t1.item_id WHERE t1.out>0 AND t1.item_id IS NULL
I am having a problem with not been able to display all records from table1.
I have 2 tables.
Table 1 and 2 and I want to display all the records from table 1 (Even if some records donly exists on table1 and no reference in table2)
This is what I am trying and I have 2 recording in Table1 but it's only displaying 1.
1 record is joined by the name_id on table1 and table2 and the other record only exists on table1 BUT I need to display both.
Here is the query:
$query = mysql_query("SELECT
table1.name_id,
table2.name_id,
FROM `table1`
LEFT JOIN `table2` ON table1.name_id=table2.name_id
");
How can I get it so it will display all the records from table1 (The one's that are join and the ones that are not too) ?
you can use "JOIN" to fetch data from both table as
$query = mysql_query("SELECT table1.name_id, table2.name_id FROM `table1` JOIN `table2` ON `table1`.`name_id`=`table2`.`name_id` ");
I am doing a query with union from two different tables and then on the fetch_array loop I would like to know from which table out of the two I am actually grabbing, anyway without adding a flag into the table's structures. ?
The flag doesn't need to be in the table, but can easily be in the query:
SELECT 'table1' as t, ... FROM table1
UNION
SELECT 'table2' as t, ... FROM table2
...
echo $row['t'];
You don't have to select fields from a table, you can simply "select a string literal" too.
If you have columns with identical name in both tables you could use as
SELECT table1.col1 as col1, table1.col2, table1.col3 FROM table1
UNION
SELECT table2.col1 as col4, table1.col5 FROM table2
then when you do $data = fetch_assoc($q) you will have
$data["col1"] // table1.col1
$data["col2"] // table1.col2
-----------------------------
$data["col4"] //table2.col1
If I have a list of ID's that I have selected from a statement
SELECT id FROM myTable WHERE name = 'TEST'
This would return just the ids (1001, 1002, 1003, etc...) Then I want to perform another SELECT statement to retrieve all the titles for all those ids.
SELECT title FROM myTable2 WHERE id = XXXX
the id in table2 is the foreign key of table2. id in myTable is the Primary Key. How can I go about retrieving all the titles from those ids. I was thinking about storing all the results of the first select statement in an array, and then using a while loop to iterate through the list and return each result into another array, but my fear is that when the database gets big if it has to return 1000 rows that could be some bad overhead. So in PHP or SQL what is the best way to perform this?
You can use a subquery:
SELECT title
FROM myTable2
WHERE id IN (
SELECT id
FROM myTable
WHERE name = 'TEST'
)
Another way to do it would to be use a JOIN, to avoid the sub-query:
SELECT title
FROM myTable2
LEFT JOIN myTable
ON myTable.id = myTable2.id
WHERE myTable.name = 'TEST'
You should just be able to select them at the same time.
SELECT a.id, b.title
FROM myTable a, myTable2 b
WHERE a.name = 'TEST' AND b.id = a.id;
to select both:
SELECT id, title FROM mytable WHERE name="TEST"
or to select the whole row
SELECT * FROM mytable WHERE name="TEST"
if its two tables you are selecting from:
SELECT id, title FROM mytable A JOIN mytable2 B USING (id)