I am working on a project where users table has column Country,state and city. and another table named projects also contains the column country,state and city.
Now i want to get the most relevent results from project table where projects should have same city,state and country. means the row where city.state and country all match should be on top. and which only match country should be at bottom.
users
ID | username | country | state | city
1 | user1 | country1| state1 |city1
2 | user2 | country2| state2 |city2
3 | user3 | country3| state3 |city3
projects
title | country | state | city
p1 | country1| state1 |city1
p2 | country1| state1 |city2
p3 | country1| state2 |city2
Suppose user1 is login and i want to show most relevent results to this user according to user location.
e.g in project taqble in row p1 all country state and city mis matching with user country state and city. in p2 two only country and state is matching and in p3 only country is matching. what i want to do it to retrieve row in in this order p1,p2 and p3.
Try this, hope help for you;)
SQL Fiddle
MySQL 5.6 Schema:
CREATE TABLE users
(`ID` int, `username` varchar(5), `country` varchar(8), `state` varchar(6), `city` varchar(5))
;
INSERT INTO users
(`ID`, `username`, `country`, `state`, `city`)
VALUES
(1, 'user1', 'country1', 'state1', 'city1'),
(2, 'user2', 'country2', 'state2', 'city2'),
(3, 'user3', 'country3', 'state3', 'city3')
;
CREATE TABLE projects
(`title` varchar(2), `country` varchar(8), `state` varchar(6), `city` varchar(5))
;
INSERT INTO projects
(`title`, `country`, `state`, `city`)
VALUES
('p1', 'country1', 'state1', 'city1'),
('p2', 'country1', 'state1', 'city2'),
('p3', 'country1', 'state2', 'city2')
;
Query 1:
select t1.*, t2.*, (t1.country = t2.country) + (t1.state = t2.state) + (t1.city = t2.city) as cnt
from users t1
left join projects t2
on t1.country = t2.country or t1.state = t2.state or t1.city = t2.city
order by t1.id, cnt desc
Results:
| ID | username | country | state | city | title | country | state | city | cnt |
|----|----------|----------|--------|-------|--------|----------|--------|--------|--------|
| 1 | user1 | country1 | state1 | city1 | p1 | country1 | state1 | city1 | 3 |
| 1 | user1 | country1 | state1 | city1 | p2 | country1 | state1 | city2 | 2 |
| 1 | user1 | country1 | state1 | city1 | p3 | country1 | state2 | city2 | 1 |
| 2 | user2 | country2 | state2 | city2 | p3 | country1 | state2 | city2 | 2 |
| 2 | user2 | country2 | state2 | city2 | p2 | country1 | state1 | city2 | 1 |
| 3 | user3 | country3 | state3 | city3 | (null) | (null) | (null) | (null) | (null) |
Related
Hello guys I have 4 tables as an key-value store in mysql
t1 (article): t2:
| id | date | | id | key | value |
------------- ---------------------------
| 1 | 2016 | | 1 | title | title1 |
| 2 | 2017 | | 1 | user_id | 1 |
| 3 | 2018 | | 2 | title | title2 |
------------- | 2 | user_id | 2 |
| 3 | title | title3 |
| 3 | user_id | 1 |
---------------------------
t1 (user): t2:
| id | date | | id | key | value |
------------- -------------------------
| 1 | NULL | | 1 | name | user1 |
| 2 | NULL | | 2 | name | user2 |
------------- -------------------------
SELECT t1.id,
GROUP_CONCAT(IF(t2.key='title',t2.value,NULL)) AS title,
t1.date,
GROUP_CONCAT(IF(t2.key='user_id',t2.value,NULL)) AS user_id,
(
SELECT GROUP_CONCAT(IF(t4.key='user_name',t4.value,NULL))
FROM t4
GROUP BY t4.id
HAVING t4.id = user_id
) AS user_name
FROM t1
INNER JOIN t2
ON t1.id = t2.id
GROUP BY t1.id
i want to print out the name of the user that is stored as an id in t2 like:
| id | title | date | user_id | user_name |
------------------------------------------------
| 1 | title1 | 2016 | 1 | user1 |
| 2 | title2 | 2017 | 2 | user2 |
| 3 | title3 | 2018 | 1 | user1 |
------------------------------------------------
i have tested WHERE clause and HAVING clause, but nothing works for me.
I found your table references way too confusing, so I used an interpretation of the sample data. I only needed 3 of the 4 tables by the way. Demo
MySQL 5.6 Schema Setup:
CREATE TABLE articles
(`id` int, `date` date)
;
INSERT INTO articles
(`id`, `date`)
VALUES
(1, '2016-01-01'),
(2, '2017-01-01'),
(3, '2018-01-01')
;
CREATE TABLE users
(`id` int, `date` date)
;
INSERT INTO users
(`id`, `date`)
VALUES
(1, NULL),
(2, NULL)
;
CREATE TABLE t2_upper
(`id` int, `key` varchar(7), `value` varchar(6))
;
INSERT INTO t2_upper
(`id`, `key`, `value`)
VALUES
(1, 'title', 'title1'),
(1, 'user_id', '1'),
(2, 'title', 'title2'),
(2, 'user_id', '2'),
(3, 'title', 'title3'),
(3, 'user_id', '1')
;
CREATE TABLE t2_lower
(`id` int, `key` varchar(4), `value` varchar(5))
;
INSERT INTO t2_lower
(`id`, `key`, `value`)
VALUES
(1, 'name', 'user1'),
(2, 'name', 'user2')
;
Query 1:
select a.id, tn.value article_title, a.date, tu.id user_id, u.value user_name
from articles a
inner join (
select
*
from t2_upper
where `key` = 'title'
) tn on a.id = tn.id
inner join (
select
*
from t2_upper
where `key` = 'user_id'
) tu on a.id = tu.id
inner join (
select
*
from t2_lower
where `key` = 'name'
) u on tu.value = u.id
Results:
| id | article_title | date | user_id | user_name |
|----|---------------|------------|---------|-----------|
| 1 | title1 | 2016-01-01 | 1 | user1 |
| 2 | title2 | 2017-01-01 | 2 | user2 |
| 3 | title3 | 2018-01-01 | 3 | user1 |
I'm doing a searcher for my webpage and i'm facing a little issue, i want to get the info from two non relationals tables but the data is not returning the way i want
table 1
ID |FNAME |LNAME | STATE | CITY
------------------------------
1 |xxxxx1 |xxxxx1| xxxx1 | xx1
2 |xxxxx2 |xxxxx2| toronto| xx2
3 |xxxxx3 |xxxxx3| xxxx3 | yy3
4 |zzzzz3 |zzzzz3| toronto| yy3
table 2
ID |NAME | STATE | CITY
---------------------
1 |yyyyy1 | yyyy1 | yy1
2 |yyyyy2 | yyyy2 | yy2
3 |yyyyy3 | toronto| yy3
currently i have
SELECT
e.id_client, e.fname_client, e.city_client, e.state_client , m.id_client, m.fname_client, m.lname_client, m.state_client, m.city_client
FROM
empresas e
CROSS JOIN
medicos m
WHERE
e.fname_client LIKE :busqueda
OR
e.city_client LIKE :busqueda
OR
m.fname_client LIKE :busqueda
OR
m.lname_client LIKE :busqueda
OR
m.state_client LIKE :busqueda
OR
m.city_client LIKE :busqueda
this displays the result like this
ID| FNAME |LNAME | STATE | CITY |ID|FNAME |LNAME | STATE | CITY
3 |yyyyy3 | | yyyy3 | yy3 |3 |xxxxx3 |xxxxx3| xxxx3 | yy3
but i want it like this
ID|FNAME |LNAME | STATE | CITY
3 |xxxxx3 |xxxxx3| xxxx3 | yy3
3 |yyyyy3 | | yyyy3 | yy3
EDIT:
with the UNION asnwer i get the data with the format i want, but is showing only the results from one table when i search for commons values, for example:
if i type "Toronto", this must shows
ID|FNAME |LNAME | STATE | CITY
2 |xxxxx3 |xxxxx3| toronto | yy3 <- doctor
3 |yyyyy3 | | toronto | yy3 <- organization
4 |zzzzz3 |zzzzz3| toronto | yy3
but is only showing this
ID|FNAME |LNAME | STATE | CITY
2 |xxxxx3 |xxxxx3| toronto | yy3 <- doctor
4 |zzzzz3 |zzzzz3| toronto | yy3 <- doctor
I guess you want to concatenate the two tables. Then...
select
id, fname, lname, state, city
from
table1
where
<your where condition here>
union all
select
id, fname, lname, state, city
from
table2
where
<your other where condition here>
EDIT
If you have:
SQL> select * from table1 order by id;
id | fname | lname | state | city
----+------------+------------+------------+------------
1 | xxxxx1 | xxxxx1 | xxxx1 | xx1
2 | xxxxx2 | xxxxx2 | toronto | xx2
3 | xxxxx3 | xxxxx3 | xxxx3 | xx3
4 | zzzzz3 | zzzzz3 | toronto | yy3
SQL> select * from table2 order by id;
id | name | state | city
----+------------+------------+------------
1 | yyyyy1 | yyyy1 | yy1
2 | yyyyy2 | yyyy2 | yy2
3 | yyyyy3 | toronto | yy3
then:
select
id, fname, lname, state, city
from
table1
where
state='toronto'
union all
select
id, name as fname, NULL as lname, state, city
from
table2
where
state='toronto'
order by id;
id | fname | lname | state | city
----+------------+------------+------------+------------
2 | xxxxx2 | xxxxx2 | toronto | xx2
3 | yyyyy3 | (null) | toronto | yy3
4 | zzzzz3 | zzzzz3 | toronto | yy3
I have a table like
Name | Image | Points | Country
-------------------------------
Bob | a.jpg | 100 | USA
Bob | b.jpg | 56 | USA
Sal | c.jpg | 87 | UK
Jim | d.jpg | 34 | UK
Bet | e.jpg | 23 | USA
Bren | f.jpg | 5 | USA
Bren | g.jpg | 15 | UK
Test | h.jpg | 10 | USA
I want to get the top 4 highest rows based on the "Points" column where the country is "USA" and removing duplicate "Names", so the desired outcome would be
Name | Image | Points | Country
-------------------------------
Bob | a.jpg | 100 | USA
Bet | e.jpg | 23 | USA
Test | h.jpg | 10 | USA
Bren | f.jpg | 5 | USA
Any help would be appreciated thank you
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(image VARCHAR(12) NOT NULL PRIMARY KEY
,name VARCHAR(12) NOT NULL
,points INT NOT NULL
,country VARCHAR(12) NOT NULL
);
INSERT INTO my_table VALUES
('a.jpg','Bob' ,100,'USA'),
('b.jpg','Bob' , 56,'USA'),
('c.jpg','Sal' , 87,'UK'),
('d.jpg','Jim' , 34,'UK'),
('e.jpg','Bet' , 23,'USA'),
('f.jpg','Bren', 5,'USA'),
('g.jpg','Bren', 15,'UK'),
('h.jpg','Test', 10,'USA');
SELECT a.*
FROM my_table a
JOIN
( SELECT name,MAX(points) points FROM my_table WHERE country = 'USA' GROUP BY name ) b
ON b.name = a.name
AND b.points = a.points
ORDER
BY points DESC
LIMIT 4;
+-------+------+--------+---------+
| image | name | points | country |
+-------+------+--------+---------+
| a.jpg | Bob | 100 | USA |
| e.jpg | Bet | 23 | USA |
| h.jpg | Test | 10 | USA |
| f.jpg | Bren | 5 | USA |
+-------+------+--------+---------+
select table.* from table join
(select Name, max(Points) mp from table where Country='USA' group by Name
order by mp desc limit 4) t
on table.Name = t.Name and table.Points = t.mp and table.Country='USA'
order by table.points desc
When a person's max point row have two record, eg bob's b.jpg is also 100, this would lead to multi rows in bobs result.
Try This:
SELECT Name,Image,MAX(points),country
FROM table_1
ORDER BY points desc
GROUP BY Name,points
LIMIT 4
I have 3 tables:
MySQL: I need to link these tables and extracted sum of one of the column per row in the resulting table
tblRedemption
+---------------------+
| OrderID | OrderDesc |
+---------------------+
| 1000001 | aaaa |
| 1000002 | bbbb |
| 1000003 | cccc |
| 1000004 | dddd |
+---------------------+
tblLink
+------------------------+
| SubmissionID | OrderID |
+------------------------+
| 201 | 1000001 |
| 202 | 1000002 |
| 203 | 1000003 |
| 204 | 1000004 |
+------------------------+
tblSubmissions
+-------------------------------------------+
| SubmissionID | Name | Mobile | Amount |
+-------------------------------------------+
| 150 | Amy | 1111111111 | 10 |
| 200 | Bob | 2222222222 | 20 |
| 201 | Carl | 3333333333 | 30 |
| 202 | Dave | 4444444444 | 10 |
| 203 | Carl | 3333333333 | 25 |
| 204 | Fin | 5555555555 | 35 |
+-------------------------------------------+
Expected Results:
+---------------------------------------------------------------------+
| SubmissionID | Name | mobile | OrderDesc | Amount | TotalAmount |
+---------------------------------------------------------------------+
| 201 | Carl | 3333333333 | aaaa | 30 | 55 |
| 203 | Carl | 3333333333 | bbbb | 25 | 55 |
| 204 | Fin | 5555555555 | cccc | 35 | 35 |
| 202 | Dave | 4444444444 | dddd | 10 | 10 |
+---------------------------------------------------------------------+
Number of tblSubmission is a bigger list than the tblredemption.
There are customers with multiple entries tblSubmission identified by their mobile (see Carl).
The table tbLink joins the 2 tblSubmission and tblRdemption.
The resulting table also needs a new column which holds the total amount for each particular customer (by mobile).
The expected results needs to include:
The SubmissionID and OrderDesc from the "tblRedemption" table.
The Name, Mobile and Amount from the tblSubmissions table.
All my queries ether gives me a small subset or just one row, or "multiple rows returned".
Any ideas?
Try this:
select l.submissionid, s.name, s.mobile, s.amount, t.totalamount
from tbllink l
inner join tblsubmissions s on l.submissionid = s.submissionid
inner join (
select `name`, sum(amount) as totalamount
from tblsubmissions
group by `name`
) t on s.`name` = t.`name`
Example: http://sqlfiddle.com/#!9/2ea56a/5
Same query totalled by mobile number
select l.submissionid, s.name, s.mobile, s.amount, t.totalamount
from tbllink l
inner join tblsubmissions s on l.submissionid = s.submissionid
inner join (
select mobile, sum(amount) as totalamount
from tblsubmissions
group by mobile
) t on s.mobile = t.mobile
EDIT:
Including order desc is relatively easy also. Let's see:
select l.submissionid, s.name, s.mobile, r.orderdesc, s.amount, t.totalamount
from tbllink l
inner join tblredemption r on l.orderid = r.orderid
inner join tblsubmissions s on l.submissionid = s.submissionid
inner join (
select `name`, sum(amount) as totalamount
from tblsubmissions
group by `name`
) t on s.`name` = t.`name`
Example: http://sqlfiddle.com/#!9/63540/2
Tables
create table tblredemption (orderid int, orderdesc varchar(100));
insert into tblredemption values (1000001, 'aaaa'), (1000002, 'bbbb'), (1000003, 'cccc'), (1000004, 'dddd');
create table tbllink (submissionid int, orderid int);
insert into tbllink values (201,1000001), (202,1000002), (203,1000003), (204,1000004);
create table tblsubmissions (
submissionid int,
`name` varchar(20),
mobile varchar(20),
amount int
);
insert into tblsubmissions values
( 150 , 'Amy' , '1111111111', 10 ),
( 200 , 'Bob' , '2222222222' , 20 ),
( 201 , 'Carl' , '3333333333' , 30 ),
( 202 , 'Dave' , '4444444444' , 10 ),
( 203 , 'Carl' , '3333333333' , 25 ),
( 204 , 'Fin' , '5555555555' , 35 );
Results:
| submissionid | name | mobile | orderdesc | amount | totalamount |
|--------------|------|------------|-----------|--------|-------------|
| 201 | Carl | 3333333333 | aaaa | 30 | 55 |
| 203 | Carl | 3333333333 | cccc | 25 | 55 |
| 202 | Dave | 4444444444 | bbbb | 10 | 10 |
| 204 | Fin | 5555555555 | dddd | 35 | 35 |
I have 3 mysql tables containing a few fields. Please refer to the following
-------- -------- --------
table 1 | table 2 | table 3
-------- -------- --------
a_id | b_id | email
| a_id
name | status | b_id
email_1 | date | name
phone | ref | phone
address | email_2 | address
state | from | state
| status
| date
| ref
| from
The email_1 and email_2 is exactly same.
I need to populate the table3 fields with all table1 and table2 data. But I need to store them in one row based on email. So they look like following:
=================================================================================
table 3
=================================================================================
email | a_id | b_id | name | phone | address | state | status | date | ref | from
------+------+------+------+-------+---------+-------+--------+------+-----+-----
a#x.co| 9 | 112 | John | 999 | xxxx | NY | 0 | 15Jue| dave| y
------+------+------+------+-------+---------+-------+--------+------+-----+-----
b#x.co| 6 | 338 | Sue | 909 | xxxx | NY | 1 | 12Jue| kell| z
------+------+------+------+-------+---------+-------+--------+------+-----+-----
c#x.co| 3 | 152 | John | 679 | xxxx | NY | 1 | 10Jue| lois| g
------+------+------+------+-------+---------+-------+--------+------+-----+-----
d#x.co| 8 | 145 | John | 599 | xxxx | NY | 0 | 8Jue | sue | f
I can't figure it out how to do that. I'm using core php, mysql. Any help please?
Thanks!
I would use something like this for mysql:
insert into table_3
(
`email`,
`a_id`,
`b_id`,
`name`,
`phone`,
`address`,
`state`,
`status`,
`date`,
`ref`,
`from`
)
select
`a`.`email_1`,
`a`.`a_id`,
`b`.`b_id`,
`a`.`name`,
`a`.`phone`,
`a`.`address`,
`a`.`state`,
`b`.`status`,
`b`.`date`,
`b`.`ref`,
`b`.`from`
from
table_1 as `a`
inner join
table_2 as `b` on `a`.`email_1` = `b`.`email_2`
But you should probably go read up on MySQL insert syntax to see it's power, and on joining data :)
insert into table_3(`email`,`a_id`,`b_id`,`name`,`phone`,`address`,`state`,`status`,`date`,`ref`,`from`)
select `email_1`,`a_id`,`b_id`,`name`,`phone`,`address`,`state`,`status`,`date`,`ref`,`from`
from table_1,table_2
where email_1=email_2;