Hi i have two table one is parentlist and childlist.
I have have to apply searching on this table by parentname and childname. I have provided my table structure for better understanding, I have to apply searching on two different field of with different field name.
Fieldname is name in parentlist table and childname in childlist table.
I want below output if I type va then parentlist and childlist record should come in that query like below example. With this va Srting i have a
parentname varu123 and childname varu123 so I want these two record after executing the query.
This is the name of First table with fieldname
parentlist
............................................................
id name mobilenumber user_jid email
............................................................
1 varu123 123456 abc21 abc#gmail.com
2 abhishesk 123456 abc21 def#gmail.com
3 harsh 1234 def22 123#gmail.com
This is the name of Second table with fieldname
childlist
..........................................
id user_id childname Shoolname
...........................................
1 1 ram St.paul
2 1 raj St.xavier
3 2 varu123 St.paul
4 2 arun St.xavier
5 3 kapil St.paul
6 3 kamal St.xavier
I want this output: .
........................................................................................................
id name mobilenumber user_jid email childname Shoolname
..........................................................................................................
1 varu123 123456 abc21 abc#gmail.com ram,raj St.paul,St.xavier
2 abhishesk 123456 abc21 def#gmail.com varu123,arun St.paul,St.xavier
select pl.*, GROUP_CONCAT(cl.childname), GROUP_CONCAT(cl.Shoolname)
from parentlist as pl
inner join childlist as cl on pl.id=cl.user_id
where pl.name like '%va%' or cl.childname like '%va%'
use MySQL inner join or where :-
select * from parentlist inner join
childlist on parentlist.id=childlist.user_id
where childname='varu123' or parentlist.name='varu123'
use MySQL inner join or like :-
select * from parentlist inner join
childlist on parentlist.id=childlist.user_id
where childname like '%varu123%' or parentlist.name like '%varu123%'
Related
I have chat database contains from three table (users,message,message_recipient)
and their relations here :
Database Design
the content of table is :
tabel users:
us_id username password
1 user1 pass1
2 user2 pass2
3 user3 pass3
table mesasage
id msg_body create_date creator_id
1 "Hi user2" --------- 1
2 "Hi user3 --------- 1
table message_recipient
id repient_id message_id is_read
1 2 1 0
2 3 2 0
my problem is when select the messages that user1 send to user2 the AND operator not working or there is bug with AND
the SQL Query :
SELECT message.id, message.msg_body,message.creator_id,message_recipient.recipient_id
FROM message,message_recipient
WHERE message.creator_id='1' AND message_recipient.recipient_id='2'
the output is:
id msg_body creator_id recipient_id
1 "Hi user2" 1 2
2 "Hi user3" 1 2
You need to use JOIN to do that:-
SELECT message.id, message.msg_body,message.creator_id,message_recipient.recipient_id
FROM message JOIN message_recipient ON message.id = message_recipient.message_id
WHERE message.creator_id='1' AND message_recipient.recipient_id='2'
Note:- When you are trying to get data from two or more tables then you need to use different types of Joins.
Reference:- https://www.w3schools.com/sql/sql_join.asp
You need to join them on message id. Try this:
SELECT m.id, m.msg_body, m.creator_id, mr.recipient_id
FROM message m
JOIN message_recipient mr on m.id=mr.id
WHERE m.creator_id='1' AND mr.recipient_id='2'
I added this in where clause message.id = message_recipient.message_id in your code and it's works
we can able to join multiple table using where clause .
SELECT message.id,
message.msg_body,message.creator_id,message_recipient.recipient_id FROM
message,message_recipient WHERE message.id = message_recipient.message_id and
message.creator_id='1' AND message_recipient.recipient_id='2'
I just want to know one SQL query.
I've got two tables:
id | id_town
1 | 26
id | town
26 | Prague
What's the query if I need to print "Prague"?
Thank you.
PhpMyAdmin prints #1052 - Column 'id' in on clause is ambiguous
I tried this
SELECT town FROM localities JOIN towns ON id = id_town
TRY THIS:
select b.town
from table1 a
inner join table2 b on a.id_town = b.id
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
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 .
I wanted to combine few result into 1 table to be shown in PHP, bad thing is there is no unique key to identify it, Here is my mysql table data:-
ServiceGroup Name Moperator ShortCode
-------------- --------- ----------------- ---------
INTEGRAT norman KW_DTAC 11111
KW_I2C norman KW_ZAIN 00000
AB_MK norman HUT/TRUEMOVE 4541311
INTERGRAT amanda KW_DTAC 11111
In the table listed, the name amanda and norman has the same value, however I want the result to be shown like this on PHP.
ServiceGroup Name Moperator ShortCode
-------------- --------- ----------- ---------
INTEGRAT norman,amanda KW_DTAC 11111
KW_I2C norman KW_ZAIN 00000
AB_MK norman HUT/TRUEMOVE 4541311
How should I put it on PHP and what should the query looks like? This is example my query:-
SELECT Distinct userfilters.ServiceGroup, userdetails.Login, userfilters.Moperator, userfilters.ShortCode FROM userdetails INNER JOIN userfilters ON userfilters.UserDetailsID=userdetails.UserDetailsID AND Login IN ('amanda', 'brandon', 'bryan', 'christina', 'johnny', 'larry', 'monica', 'norman', 'eko') ORDER BY ServiceGroup DESC
Group the data by the unique columns and use grop_concat to put the login in a comma seperated list
SELECT f.ServiceGroup,
group_concat(d.Login) as logins,
f.Moperator,
f.ShortCode
FROM userdetails d
INNER JOIN userfilters f ON f.UserDetailsID = d.UserDetailsID
WHERE d.Login IN ('amanda', 'brandon', 'bryan', 'christina', 'johnny', 'larry', 'monica', 'norman', 'eko')
GROUP by f.ServiceGroup, f.Moperator, f.ShortCode
ORDER BY f.ServiceGroup DESC