join 2 tables to list results - php

I have 2 tables and I need join these tables to show the latest articles from my site
Table Posts
ID ID_CAT ID_USER
10876 5 3489
10877 6 3453
10878 1 2356
10879 4 6789
10880 8 2348
10881 9 8327
Table Posts2
ID ID_CAT ID_USER
10882 7 3989
10883 2 3473
10884 7 2246
The result should be
ID ID_CAT ID_USER
10876 5 3489
10877 6 3453
10878 1 2356
10879 4 6789
10880 8 2348
10881 9 8327
10882 7 3989
10883 2 3473
10884 7 2246
I have this query from this similar article but not works.
:https://dba.stackexchange.com/questions/11367/how-to-join-two-table-and-show-one-query-result-in-mysql
SELECT name FROM
(
SELECT name FROM table1
UNION
SELECT name FROM table2
) A;

You didn't substitute in your own columns for this, try this query:
SELECT * FROM ( SELECT * FROM Posts UNION SELECT * FROM Posts2) AllPosts;

Related

How to Get Newest Data with `created_at` Column

How can I query the data where id_konsul = 4, and how can I get the latest staflow from created_at if it is the same?
example
id id_konsul staflow created_at
1 4 1 21/05/2018 11.03
2 4 2 22/05/2018 11.03
3 4 3 23/05/2018 11.03
4 4 4 24/05/2018 11.03
5 4 5 25/05/2018 11.03
6 4 6 26/05/2018 11.03
7 4 7 27/05/2018 11.03
8 4 6 28/05/2018 11.03
9 4 7 29/05/2018 11.03
10 4 7 30/05/2018 11.03
11 4 8 31/05/2018 11.03
than i get this
id id_konsul staflow created_at
1 4 1 21/05/2018 11.03
2 4 2 22/05/2018 11.03
3 4 3 23/05/2018 11.03
4 4 4 24/05/2018 11.03
5 4 5 25/05/2018 11.03
8 4 6 28/05/2018 11.03
10 4 7 30/05/2018 11.03
11 4 8 31/05/2018 11.03
try this query
SELECT * FROM test1 n
WHERE created_at=(SELECT MAX(created_at)FROM test1
WHERE staflow=n.staflow)
order by id
To get latest row per staflow attribute you can use a self join
select a.*
from demo a
join (
select id_konsul,staflow, max(created_at) created_at
from demo
where id_konsul = 4
group by id_konsul, staflow
) b on a.staflow = b.staflow
and a.id_konsul = b.id_konsul
and a.created_at = b.created_at;
Or using a left join
select a.*
from demo a
left join demo b on a.id_konsul = b.id_konsul
and a.staflow = b.staflow
and a.created_at < b.created_at
where a.id_konsul = 4
and b.id_konsul is null
Demo
To write above queries using laravel's query builder you can use following references
Laravel Eloquent select all rows with max created_at
Laravel - Get the last entry of each UID type
Laravel Eloquent group by most recent record
Please try this query.
SELECT * FROM table_name WHERE id_konsul = 4 ORDER BY created_at DESC LIMIT 1;
You can user INNER JOIN.
Example:
SELECT t0.* from {TABLE_NAME} AS t0 INNER JOIN {TABLE_NAME} t1 ON t0.staflow = t1.staflow WHERE t0.created_at > t1.created_at and t0.id_konsul = 4

Count pass, fail, skip number according to id using PHP and MySQL

I have four tables look like below:
test_case (1='pass',2='fail',3='skip')
id testing_status
1 1
2 3
3 1
4 2
5 3
6 2
7 2
8 3
test_suite_with_case (referenced with test_case_id in test_case table and test_suite_id with test_suite table)
id test_suite_id test_case_id
1 4 1,3,5,2,6,7,8
2 3 2,5,4,6,7
test_suite
id test_suite_name
3 test_1
4 test_2
test_suite_run (referenced with test_suite_id in test_suite table)
id test_suite_id name
1 3 BBH
2 4 CXN
Now i want to run a where query from test_suite_run by id (for example id=2 in test_suite_run) and want a output look like below:
id(test_suite_run) name test_suite_name pass fail skip
1 CXN test_suite_2 2 2 3
I am new in PHP and MySQL.
Try this:
select
tr.id,
tr.name,
ts.test_suite_name,
sum(testing_status = 1) pass,
sum(testing_status = 2) fail,
sum(testing_status = 3) skip
from test_suite_run tr
inner join test_suite ts on tr.test_suite_id = ts.id
inner join test_suite_with_case tsc on ts.id = tsc.test_suite_id
inner join test_case tc on find_in_set(tc.id, tsc.test_case_id) > 0
group by
tr.id,
tr.name,
ts.test_suite_name;

Sql query to show count(*) of groups while keeping all rows of the group

I have a table similar the following
id user_id father_id
1 1 1
2 2 1
3 3 2
4 4 2
5 5 2
6 6 3
7 7 4
I search for a sql query (prefer Fluent or Eloquent for Laravel 4) to give me the following result:
id user_id father_id family_members
3 3 2 3
4 4 2 3
5 5 2 3
1 1 1 2
2 2 1 2
6 6 3 1
7 7 4 1
As it can be observed family_members is the count of users who have the same father_id
select id, user_id, father_id, count(*) as family_members from users group by father_id
The query above, just keeps the top row of each group, but I want to keep all the other records and not only the first one; then sorting them first according to the family_members and then according to father_id
How can I achieve it?
I donĀ“t know Fluent, nor Eloquent, nor Laravel 4, but the sql query would be like this.
SELECT yourTable.*, auxTable.family_members
FROM yourTable
LEFT JOIN
(
SELECT father_id, COUNT(id) as family_members
FROM yourTable
GROUP BY father_id
)auxTable ON yourTable.father_id = auxTable.father_id
ORDER BY family_members DESC, yourTable.father_id ASC
I solved the problem by the following Fluent query based on the answer from segio0983
$users = DB::table('users')
->leftjoin(DB::raw('(SELECT father_id, COUNT(id) as family_members
FROM users
GROUP BY father_id) auxTable '),function($join)
{
$join->on('users.father_id', '=', 'auxTable.father_id');
})
->orderBy('auxTable.family_members','desc')
->orderBy('users.father_id','asc')
->select('users.id','users.father_id','auxTable.family_members');

Merge results from two different tables with different column number, ORDER them by same key

I have two tables:
Table A: ID Cars Planes Num
1 3 5 2
2 8 44 1
3 7 23 6
4 6 2 7
Table B: ID Horses Dogs Cats Elefants Num
1 3 5 2 3 3
2 8 44 1 22 4
3 7 23 4 14 8
4 6 2 3 15 5
What I need to do: I need to get all results from both tables and sort them by the "Num" Column, where the "number" actually is unique for each result from both rows.
Is it even possible to "merge" those two tables and order them by "num" or should I just get each table separately ordered and do two loops checking always for the next num jumping between tables?
Thanks
you can merge them like that with UNION .
try this:
select num from(
select num from table1
union all
select num from table2
)t
order by num asc
DEMO HERE
EDIT:
select id ,Cars,Planes, Horses,Dogs,Cats,Elefants,num from(
select id ,Cars,Planes,'No horses' Horses,'No dogs' Dogs,'No cats' Cats,'No elefants' Elefants,num from table1
union all
select id,'No cars' Cars,'No planes' Planes,Horses,dogs,Cats,Elefants, num from table2
)t
order by num asc;
DEmo with other columns
SELECT NUM FROM TABLEA
UNION ALL
SELECT NUM FROM TABLEB
ORDER BY 1

Mysql SELECT for friends system

I have this table
conncections
id_user connectedto
1 4
2 4
3 1
1 5
i would like to do a SELECT that gives me back all the numbers that have a relation with the number 1 and the number 1 itself. Therefore 1 4 3 5 as result.
Something like
SELECT DISTINCT id_user as related
FROM connections
WHERE connectedto = 1
UNION
SELECT DISTINCT connectedto as related
FROM connections
WHERE id_user = 1
With UNION duplicate are removed

Categories