I have a problem with sql query. I need join three tables, count rows, get last row, and the create custom column.
The example my tables like below:
table name: article
_______________________________________
idarticle idwriter title
---------------------------------------
1 1 Title One
2 3 Title Two
3 2 Title Three
table name: comment
________________________________________________________________________________
idcomment idarticle idcommented content datetime
--------------------------------------------------------------------------------
1 1 2 Comment One 2015-05-15 00:00:00
2 1 3 Comment Two 2015-05-16 00:00:00
3 1 1 Comment Three 2015-05-17 00:00:00
4 2 2 Comment Four 2015-05-18 00:00:00
5 3 3 Comment Five 2015-05-19 00:00:00
6 3 2 Comment Six 2015-05-20 00:00:00
table name: member
_____________________
idmember username
---------------------
1 apple
2 orange
3 banana
How to join all tables, count, and get last commented+username with one query.
May the result like:
_____________________________________________________________________________________________________________________
idarticle idwriter title username_writer totalcomments lastcomment_id lastcomment_username lastcomment_datetime
---------------------------------------------------------------------------------------------------------------------
3 2 Title Three orange 2 2 orange 2015-05-20 00:00:00
2 3 Title Two banana 1 2 orange 2015-05-18 00:00:00
1 1 Title One apple 3 1 apple 2015-05-17 00:00:00
I hope someone will solve my problem.
I using PHP 5.4, MySQL 5.5, and MeekroDB library, so the query must support with those.
Sorry for my English.
Thank you
http://sqlfiddle.com/#!9/132336/1
SELECT a.idarticle, a.idwriter , a.title,
m.username,
c.idcomment, cm.username, c.datetime
FROM article a
LEFT JOIN member m
ON a.idwriter = m.idmember
LEFT JOIN comment c
ON a.idarticle = c.idarticle
LEFT JOIN comment c1
ON a.idarticle = c1.idarticle
AND c.datetime<c1.datetime
LEFT JOIN member cm
ON c.idcommented = cm.idmember
WHERE c1.datetime IS NULL
Related
I have two tables. I want to combine them but include rows that don't return a value. This question is probably going to be flagged as duplicate or something, but I have already tried reading the other posts and still failed. So I might be below the average MySQL Programmer. Hope somebody can help.
table_price_list
item_id price_type_id price_amount
1 1 100.00
1 2 95.00
1 3 90.00
1 4 85.00
1 5 80.00
1 6 75.00
2 1 201.56
2 2 196.45
2 3 191.78
2 4 186.36
3 1 1210.12
3 2 1205.45
3 3 1200.69
3 4 1195.48
3 5 1190.98
table_price_type
price_type_id price_type
1 srp
2 reseller
3 distributor
4 mega
5 depot
6 special
Desired output
item_id price_type_id price_type
1 srp 100.00
1 reseller 95.00
1 distributor 90.00
1 mega 85.00
1 depot 80.00
1 special 75.00
2 srp 201.56
2 reseller 196.45
2 distributor 191.78
2 mega 186.36
2 depot null
2 special null
3 srp 1210.12
3 reseller 1205.45
3 distributor 1200.69
3 mega 1195.48
3 depot 1190.98
3 special null
The best I could get so far is this, this leaves out the blank price_type
select b.item_id, a.price_type, b.price_amount
from table_price_type A
left outer join table_price_list B on A.price_type_id=B.price_type_id
It doesn't have to be null, it could just be blank (' ').
You can use left join to get required result.
select b.item_id, a.price_type, b.price_amount
from table_price_type A
left join table_price_list B on A.price_type_id=B.price_type_id
I have two tables. One is ps_product_lang and ps_category_product. The data inside ps_category_product is like this
id_category id_product
2 1
2 2
2 3
2 4
2 5
2 6
2 7
3 1
3 2
3 3
3 4
3 5
3 6
3 7
4 1
4 2
5 1
7 2
8 3
8 4
8 5
8 6
8 7
9 3
10 4
The table for ps_product_lang is like this
id_product id_lang name
1 1 Faded Short Sleeves T-shirt
1 2 Faded Short Sleeves T-shirt
2 1 Blouse
2 2 Blouse
3 1 Printed Dress
3 2 Printed Dress
4 1 Printed Dress
4 2 Printed Dress
5 1 Printed Summer Dress
5 2 Printed Summer Dress
6 1 Printed Summer Dress
6 2 Printed Summer Dress
7 1 Printed Chiffon Dress
7 2 Printed Chiffon Dress
So here I want to get the id_product,name from ps_product_lang where id_category is 5,7 and name like 'p%'. so can someone tell me what would be the query. Any help and suggestions would be really appreceable.
As #Gordon was hinting, you simply need an INNER JOIN between the two tables, along with a where condition to restrict the product category and name:
SELECT t1.id_product,
t1.name
FROM ps_product_lang t1
INNER JOIN
(
SELECT DISTINCT id_product
FROM ps_category_product
WHERE id_category IN (5, 7)
) t2
ON t1.id_product = t2.id_product
WHERE t1.name LIKE 'P%'
Here is a demo which omits the WHERE clause to show how the query behaves when it actually returns data:
SQLFiddle
Use JOIN to get data from 2 tables
SELECT p.id_product,name
FROM ps_product_lang p
JOIN ps_category_product c
ON p.id_product=c.id_product
WHERE id_category IN (5,7)
AND name LIKE 'p%'
I am stuck in query I have a table like this
budget_details
id budget_id expenditure_head_id budget
1 1 1 1233
2 1 2 333
3 1 3 567
4 2 1 343
5 2 2 343
6 2 3 6767
7 2 4 557
expenditure_heads
id name
1 abc
2 xyz
3 qwe
4 uvw
I want to get all the expenditure_heads from budget_details that even
if not in budget_details like here budget_id=1 does not contain expenditure_head_id 4
but I want to select that to with 0 or null displaying
I tried this but it not displaying expenditure_head_id 4
select `expenditure_heads`.`name`, `budget_details`.`budget`, `budget_details`.`id` from
`budget_details`
right join `expenditure_heads` on `budget_details`.`expenditure_head_id` = `expenditure_heads`.`id`
where `budget_details`.`budget_id` = 1"
The where avoid you to get the missing row you need. The left join is done on the ON statement, so this query should work for you:
SELECT EH.name, BD.budget, BD.id FROM expenditure_heads EH
LEFT JOIN budget_details BD
ON (BD.expenditure_head_id = EH.id AND BD.budget_id = 1)
Whats the best way to check if different groups of rows in a table with the same GroupID such as different teams have a SINGLE captain? Captain Identifier for example could be '10', so its crucial that it goes through multiple records with the same groupID and checks to see if theres ONLY ONE record with the positionID as '10'. I need it to do it for all teams, i.e all groupID's
-------------------------------------------------
ID | Group ID | Name | Position|
-------------------------------------------------
1 1 John 3
2 1 jim 3
3 1 Hahn 4
4 1 Mary 4
5 1 Moe 4
6 1 Charlie 10
7 2 taylor 4
8 2 Geoff 4
9 2 adam 4
10 2 cam 10
11 3 sharon 2
12 3 tony 4
13 3 steve 3
14 3 eve 4
15 3 gwen 10
--------------------------------
So what I need it to do is check that every groupID only had ONE 10 as the position.
Thanks in advance guys. Check out the image link at the bottom.
im using mysql btw
Sorry if this is badly described
If I understood you - I think you need something like :
select groupId, sum(case when positionID='10' then 1 else 0 end) as captains
from tbl_name
group by groupID
having captains = 1
am I close???
If I understood you correctly, you want an indication that will tell you whether the groupID had more then one captain.
So what you need is this:
select groupID,case when cnt = 1 then 1 else 0 end as IND_ONE_CAPTAIN from (
select groupID, sum(case when positionid = '10' then 1 else 0 end) as cnt
from players
group by groupID)
Now IND_ONE_CAPTAIN columns consist 1 or 0, 1 is for the group id only had 1 captain, and 0 is when they had more.
I want to retrieve table with total count from multiple table and joins
I have 4 wordpress custom database tables.
wp_tutorials
ID tut_name
1 php
2 mysql
3 wordpress
wp_chapters
ID tut_id chapter_name
1 1 php1
2 1 php2
3 2 mysql1
4 2 mysql2
wp_series
ID chapter_id series_name
1 1 php1A
2 1 php1B
3 2 php2A
4 2 php2B
5 3 mysql1A
6 3 mysql1B
7 4 mysql2A
8 4 mysql2B
wp_tut_users
ID series_id user_name
1 2 user1
2 2 user2
3 4 user3
4 6 user4
5 7 user5
from these four tables I want to retrieve by sql query following table.
1. tutorial
tut_name total_users
php 3
mysql 2
wordpress 0
expecting best ways...
Use a left join to get even tutorials with no users
select t.tut_name, count(u.id) as total_users
from wp_tutorials t
left join wp_chapters c on c.tut_id = t.id
left join wp_series s on s.chapter_id = c.id
left join wp_tut_users u on u.series_id = s.id
group by t.tut_name