I have some data in multiple tables i want to join that tables and need to display the result as multiple records below is the table structure
main_table
id name height
1 test1 5.2
2 test2 4.6
child_table
id main_table_id name height
1 1 test3 5.3
2 1 test4 4.5
expecting result like
id name height
1 test1 5.2
1 test3 5.3
1 test4 4.5
How can i achive this using query in MySql expecting suggestions how can i achieve this?
Use UNION
SELECT id, name, height
FROM main_table
WHERE id = 1
UNION
SELECT main_table_id as id, name, height
FROM child_table
WHERE main_table_id = 1
Try using left and colasce
select maintable.id,COALESCE(maintable.name,childtable.name) as name,
COALESCE(maintable.height,childtable.height)
from maintable left join childtable
on maintable.id=childtable.main_table_id
where maintable.id=1
Related
I need a help with projecting my database. The purpose of this database will be to show offers in different categories. There are 1-6 categories to each item. There are around 80 categories types, so I decided to make three tables as below:
table1:
ID Item_id
1 1
2 2
3 3
4 4
5 5
table2:
ID Item_id Category
1 1 cat55
2 1 cat56
3 1 cat57
4 1 cat58
5 2 cat42
6 2 cat43
7 2 cat44
8 2 cat45
9 3 cat42
etc.
table3:
Category_id category_name
cat55 apples
cat56 oranges
cat57 bananas
cat58 pineapples
Am I doing this right? I've got a problem to make proper sql query to show my categories in php, because when I use this query:
SELECT table1.*, table2.*, table3.*
FROM table1
INNER JOIN table2
ON table1.item_id = table2.item_id
INNER JOIN table3
ON table2.category=table3.category_id
It only gives me the first category name, when I need all of them and show them like this:
Item 1: apples, oranges, bananas, pineapples
Item 2: cat42, cat43, cat44, cat45
Item 3: cat42
What am I doing wrong? Is it wrong query or I need to change the database structure to like this
table 1 and 3 unchanged
table 2:
ID Item_id c1 c2 c3 c4 c5 c6
1 1 cat55 cat56 cat57 cat58 null null
2 2 cat42 cat43 cat44 cat45 null null
3 3 cat42 null null null null null
I'm using foreach loop, so I can do only one query, I know that more queries are possible, but I need to make it as simple as possible.
If you want to fetch all names in single row for each item, following query will work:
SELECT table1.Item_Id,Group_Concat(t3.category_name separator ',') as Category_Name
FROM table1
INNER JOIN table2
ON table1.item_id = table2.item_id
INNER JOIN table3
ON table2.category=table3.category_id
Group by table1.Item_Id;
I don't find any problem in your DB Structure.
Hope it helps!
I try to select all from 2 tables in mysql database with the sum of total cost in the second table
i have a table name tbl_project contain
db_id db_projectname and other column
1 test
2 test2
3 test3
second table name tbl_activities contain
db_id db_projectname db_totalcost
1 test 200
2 test 300
3 test2 800
the out put i want is
test 500
test2 800
test3
i try this query but it didn't give me that result
select tbl_project.db_id, tbl_project.db_projectname,tbl_project.db_location,tbl_project.db_client,tbl_project.db_transferredto,tbl_project.db_psd,tbl_project.db_pdd,tbl_project.db_duration,tbl_project.db_past,tbl_project.db_padd,tbl_project.db_aduration,tbl_project.db_percent,tbl_project.db_pnote,tbl_project.db_user,tbl_project.db_cpercentage,tbl_project.db_epercentage,tbl_project.db_mpercentage,tbl_project.db_status,tbl_project.db_offer,tbl_project.db_sheet,tbl_project.db_invoice,tbl_project.db_po,sum(tbl_activities.db_totalcost) as total_cost from tbl_project,tbl_activities where
tbl_project.db_projectname=tbl_activities.db_projectname
it give me
test but sum of another project and only one project not all
You needed to use LEFT JOIN & GROUP BY
SELECT
tbl_project.db_id,
tbl_project.db_projectname,
tbl_project.db_location,
tbl_project.db_client,
tbl_project.db_transferredto,
tbl_project.db_psd,
tbl_project.db_pdd,
tbl_project.db_duration,
tbl_project.db_past,
tbl_project.db_padd,
tbl_project.db_aduration,
tbl_project.db_percent,
tbl_project.db_pnote,
tbl_project.db_user,
tbl_project.db_cpercentage,
tbl_project.db_epercentage,
tbl_project.db_mpercentage,
tbl_project.db_status,
tbl_project.db_offer,
tbl_project.db_sheet,
tbl_project.db_invoice,
tbl_project.db_po,
sum(
tbl_activities.db_totalcost
) AS total_cost
FROM
tbl_project
LEFT JOIN tbl_activities ON tbl_project.db_projectname = tbl_activities.db_projectname
GROUP BY tbl_project.db_id
Note:
Using aggregate function (e.g. SUM,COUNT..) without GROUP BY collapses the result set into a single row.
try this;
SELECT
db_projectname.db_projectname,SUM(tbl_activities.db_totalcost) AS total_cost
FROM db_projectname
LEFT JOIN tbl_activities
ON
db_projectname.db_projectname = tbl_activities.db_projectname
GROUP BY db_projectname.db_projectname
I have three tables where table_2 is the middle(connected) between table_1 and table_3
tables
table_id
...
...
table_rest
rest_id
table_id
...
rest
rest_id
...
...
And the query to select I use
SELECT m.table_id, table_name
FROM tables m
JOIN table_rest mr
ON m.table_id = mr.table_id
WHERE rest_id = '$rest_id'
What I need now is to join in this query another table reserv
id
...
status
To check if status is 0, 1,or 2 to not show me anything if there is no status this mean there is no record to show me. In other words this is resserved system where I show on screen few tables. If status is 0,1,2 thats mean the table is taken. If nothing is found for status this mean that there is no record for table and can be shown to user.
EDIT: Sample scenario
tables
table_id
1
2
3
4
5
rest
rest_id
1
2
table_rest
table_id | rest_id
1 2
2 2
3 2
4 2
5 2
So the query that is above will generate 5 tables for rest_id=2 and none for rest_id=1
So now I have another table
reserv
id | status
1 0
2 1
3 2
So in this table reserv currently are saved 3 tables. The idea is to show me other two whit id=4 and id=5 because they are not in table reserv and don't have any status.
Hope is a little bit more clear now.
You have to point from table reserv to which table is beign booked, let's call it reserv.table_id
SELECT m.table_id, table_name
FROM tables m
JOIN table_rest mr
ON m.table_id = mr.table_id
left join reserv
on reserv.table_id = m.id
WHERE rest_id = '$rest_id'
and reserv.status is null (*note)
*note use 'is' or 'is not' depending of your needs, as far as I read, first seems that you want !=, later that what you want is =
It's better if you provide sample data or sqlfiddle. Based on what I realize: Is this what you want:
select tables.table_id, rest.rest_id
from tables
left join table_rest on table_rest.table_id = tables.table_id
left join rest on rest.rest_id = table_rest.rest_id
where rest.rest_id = '$rest_id'
and tables.table_id not in (select id from reserv)
I have two table (table 1 and table 2) . I want to show all the rows from table1 by joining with table2 which have multiple rows with same table1 id (foreign key relation) and will sort the result by table2 priority column (order by desc).
Table1
Table2
Result will be
thanks in advance
Edit
Table1
id name
1 test1
2 test2
3 test5
4 test7
5 test9
6 test3
Table2
id table1_id event priority
1 2 abc 0
2 2 kbc 0
3 2 abc 2
4 2 kbc 1
5 4 fgg 2
6 4 dss 3
7 1 fgfg 2
8 5 fgfg 2
9 6 xcxc 1
10 6 fgfh 3
Result
id_table1 name event priority
4 test7 dss 3
6 test3 fgfh 3
2 test2 abc 2
1 test1 fgfg 2
5 test9 fgfg 2
3 test5 NULL NULL
In the question you mentioned you need to select the data where id from table1 is available more than once in the table2 which does not match with the result set you gave.
Considering the original requirement the following should do the trick
select
t2.table1_id as id_table1,
t1.name,
t2.priority,
t2.event
from table1 t1
join
(
select
p1.table1_id,
p1.event,
p2.priority
from table2 p1
join(
select
max(priority) as priority,
table1_id
from table2
group by table1_id having count(*) > 1
)p2
on p2.table1_id = p1.table1_id and p2.priority = p1.priority
)t2
on t1.id = t2.t1id
order by t2.priority desc
Here is a demo
The result will get the same event corresponding the max priority column
This will get the result set that you want. You mentioned that you only need the items table1 ids that reflects more than once but result query shows tableid1 "1" even though it is only present once:
SELECT DISTINCT t1.id,t1.name ,t2.event, t2.priority
FROM TABLE2 t2
right join
TABLE1 t1
on t1.id=t2.table1_id
order by t2.priority desc
Try this query:
SELECT t1.*,t2.priority FROM table1 t1
INNER JOIN table2 t2
ON t1.id=t2.id
ORDER BY t2.priority DESC
Primary key and foreign key should have the same name. The syntax should be
SELECT Table1.id_table1,Table1.name,Table2.event,Table.priority FROM
Table1 LEFT JOIN Table2 ON
Table1.id=Table2.id
ORDER BY Table2.priority DESC
Make the following changes in Table2:
Get rid of the first column or rename it
Rename second column(your foreign key) to "id".
I got two following tables
category:(one to many-----question)
id name parentId
1 test1 0
2 test2 1
3 test3 1
4 test4 3 .....
like a tree
test1
test2
test3
test4
question:
id title category_id
1 question1 1
2 .... 1
3 .... 2
my question is : if i search category id = 1 ,there will be print total questions:
count(question.id)
3
How to build Select query to do that? is that possible?
Thank you for your valuable help.
This will return count of questions for your selected category, here in example category_id = 1:
mysqli_query('SELECT COUNT(id) FROM question WHERE category_id = 1');
Updated:
so, if you want to count it also for subcategories, the simpliest way will be to have "path" in your category table, where will be all IDs (self ID and ID-s of all parents), and you can separate it with ~ (its important to have ~ also at the beginning and end of path; path can be VARCHAR(255), but if you want have really deep tree, you can use TEXT.
id name parentId path
1 test1 0 ~1~
2 test2 1 ~1~2~
3 test3 1 ~1~3~
4 test4 3 ~1~3~4~
Hope, its clear enough, how you will update your table category to have there also column path.
And the select then will be:
mysqli_query('
SELECT COUNT(id)
FROM question
WHERE category_id IN (
SELECT id
FROM category
WHERE path LIKE "%~'.$category_id.'~%"
)
');
$category_id will be ID of category, for which you want to count questions (also for subcategories).
This is a very simple query:
SELECT COUNT(*) FROM question WHERE category_id='1';
But you probably want a join query like this:
SELECT COUNT(*) FROM question INNER JOIN category
ON (question.category_id = category.id) WHERE category.name='test1';
That will give you the option of searching for category names.
(BTW, did you google this?)
Edit: took me a cup of coffee, but indeed a LEFT JOIN does not make sense if the WHERE is on the joined table. INNER JOIN does.
Try like this,
SELECT COUNT(id) FROM question WHERE category_id IN (SELECT id FROM question WHERE category_id='1')