table customers:
+----+------------+
| id | text |
+----+------------+
| 33 | name1 |
| 34 | name2 |
| 35 | name3 |
| 36 | name4 |
+----+------------+
table orders:
+----+--------+------------+----------+
| id | cid | ordername | colors |
+----+--------+------------+----------+
| 8 | 34 | name | 5 |
+----+--------+------------+----------+
Is possible with one query have this result?
+----+--------+------------+----------+
| id | cid | ordername | colors |
+----+--------+------------+----------+
| 8 | name1 | name | 5 |
+----+--------+------------+----------+
here how you can do it
select o.`id`, `text` as cid , `ordername`, `colors` from orders o
inner join customers c
on c.id = o.cid
DEMO HERE
OUTPUT:
ID CID ORDERNAME COLORS
8 name2 name 5
Related
we have two table for example persons1 and persons2 and out put should be rows of those tables
for exemple:
`persons1`
+----+-------------+
| id | name |
+----+-------------+
| 1 | john |
| 2 | Sophia |
+----+-------------+
and
`persons2`
+----+-------------+
| id | the_name |
+----+-------------+
| 1 | Olivia |
| 2 | Jackson |
+----+-------------+
Output is :
array(
[1,'john'],
[2,'Sophia'],
[1,'Olivia'],
[2,'Jackson'],
)
thank you
You can use union like this:
SELECT id, name as `the_name` FROM `persons1`
UNION
SELECT id, the_name FROM `persons2`;
which outputs:
| id | the_name |
|----| ---------|
| 1 | John |
| 2 | Sophia |
| 1 | Olivia |
| 2 | Jackson |
SqlFiddle
SELECT p1.id, p1.name AS `the_name` FROM persons1 p1
UNION
SELECT p2.id, p2.the_name FROM persons2 p2;
I have 3 tables:
table_1:
| id | test |total_employee|
+----+------+--------------+
| 1 | xxxx | 3 |
| 2 | yyyy | 2 |
| 3 | zzzz | 3 |
----------------------------
table_2:
| id | id_table1 |id_employee|
+----+-----------+-----------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
| 4 | 1 | 1 |
| 5 | 1 | 2 |
| 6 | 1 | 1 |
| 7 | 1 | 2 |
| 8 | 1 | 3 |
-----------------------------
table employee:
| id | name |
+----+------+
| 1 | emp1 |
| 2 | emp2 |
| 3 | emp3 |
-------------
and now I want to display all employees from table employee and from table_2 if the employee record in table employee exists but in table_2 it does not. If record does not exist mark as "unchecked" but if exists mark as "checked".
The point is how to find data from table_employee no matter data is exist or not exist on table_2.
The IF() operation lets you do this:
SELECT e.*, IF(ISNULL(t2.id), 'unchecked', 'checked') AS `checked`
FROM table_employee e
LEFT JOIN table_2 t2 ON t2.id_employee = e.id
This is my first question, so please be nice :)
I have this:
mysql> select cat_id, cat_name from phpbb_dm_eds_cat;
+--------+----------+
| cat_id | cat_name |
+--------+----------+
| 9 | catx |
| 10 | cat2 |
| 11 | test |
+--------+----------+
mysql> select subcat_id, subcat_name from phpbb_dm_eds_subcat;
+-----------+--------------+
| subcat_id | subcat_name |
+-----------+--------------+
| 39 | aaa |
| 40 | xxx111 |
| 41 | TESTXX |
| 42 | xxa |
+-----------+--------------+
Here is the download table:
mysql> select download_id, download_title, download_cat_id from phpbb_dm_eds;
+-------------+----------------+-----------------+
| download_id | download_title | download_cat_id |
+-------------+----------------+-----------------+
| 3 | s | 9 |
| 5 | raver | 41 |
| 6 | hans | 10 |
| 7 | readme | 42 |
+-------------+----------------+-----------------+
Now the query:
mysql>
SELECT bc.cat_name, bc.cat_id,
COUNT(bd.download_id) AS number_downloads,
MAX(bd.last_changed_time) AS last_download
FROM phpbb_dm_eds_cat bc
LEFT JOIN phpbb_dm_eds bd ON bd.download_cat_id = bc.cat_id
GROUP BY bc.cat_id ;
+----------+--------+------------------+---------------+
| cat_name | cat_id | number_downloads | last_download |
+----------+--------+------------------+---------------+
| catx | 9 | 1 | 1427072549 |
| cat2 | 10 | 1 | 1427125950 |
| test | 11 | 0 | NULL |
+----------+--------+------------------+---------------+
the 'test' category has a subcategory which has a download in the subcat table, the categories 'catx' and 'cat2' have downloads too, there the last download is displayed correctly but i want the last_download in the subcat 'test' to be displayed too
How do I write the query?
You are not clear about what you want but it might be:
SELECT bc.cat_name, bc.cat_id,
COUNT(bd.download_id) AS number_downloads,
MAX(bd.last_changed_time) AS last_download
FROM (
select cat_id, cat_name from phpbb_dm_eds_cat
union
select subcat_id as cat_id, subcat_name as cat_name
from phpbb_dm_eds_subcat
) as bc
LEFT JOIN phpbb_dm_eds bd ON bd.download_cat_id = bc.cat_id
GROUP BY bc.cat_id ;
(Your SELECT is using a MySQL extension, because cat_id is neither in the GROUP BY nor in an aggregate.)
From your notes"the 'test' category has a subcategory" ,i suggest there should be a relation between cat table "phpbb_dm_eds_cat" and sub_cat table
"phpbb_dm_eds_subcat".
Like this:
Table name
phpbb_dm_eds_cat
Columns
(cat_id,cat_name)
Tablename
phpbb_dm_eds_subcat
Columns
(cat_id referencing table phpbb_dm_eds_cat(cat_id),subcat_id,subcat_name)
Sadly your query doesn't work, it gives me ALL subcats, like this:
+----------+--------+------------------+---------------+
| cat_name | cat_id | number_downloads | last_download |
+----------+--------+------------------+---------------+
| catx | 9 | 1 | 1427072549 |
| cat2 | 10 | 1 | 1427125950 |
| test | 11 | 0 | NULL |
| aaa | 39 | 0 | NULL |
| xxx111 | 40 | 0 | NULL |
| TESTXX | 41 | 1 | 1427123713 |
| xxa | 42 | 1 | 1427136789 |
+----------+--------+------------------+---------------+
7 rows in set (0,00 sec)
I don't want the subcats to be displayed, I only want the last download from the subcat, this should be the correct output:
+----------+--------+------------------+---------------+
| cat_name | cat_id | number_downloads | last_download |
+----------+--------+------------------+---------------+
| catx | 9 | 1 | 1427072549 |
| cat2 | 10 | 1 | 1427125950 |
| test | 11 | 1 | 1427213321 |
+----------+--------+------------------+---------------+
3 rows in set (0,00 sec)
I have 2 tables.
Table 1: tA
+----+--------------------+
| id | url |
+----+--------------------+
| 1 | hxxp://www.aaa.com |
| 2 | hxxp://www.bbb.com |
+----+--------------------+
Table 2: tB
+----+-------+--------+----------+
|id | tA_id | cat_id | cat_name |
+----+-------+--------+----------+
| 1 | 1 | 1 | Mobile |
| 2 | 1 | 2 | Other |
| 3 | 2 | 2 | Other |
+----+-------+--------+----------+
Expected Output
+----+-------+--------+----------+
| id | tA_id | cat_id | cat_name |
+----+-------+--------+----------+
| 1 | 1 | 1 | Mobile |
| 3 | 2 | 2 | Other |
+----+-------+--------+----------+
mysql code:
select *
from tA
inner tB
on tA.id = tB.tA_id
where tB.cat_id = 1
or tB.cat_id = 2
Group By tA_id
result from my mysql code:
+----+-------+--------+----------+
| id | tA_id | cat_id | cat_name |
+----+-------+--------+----------+
| 1 | 1 | 2 | Other |
| 3 | 2 | 2 | Other |
+----+-------+--------+----------+
How do I do?
Remove your group by tA_id because it is grouping cat_name Mobile and Other together.
If you need to keep it use ORDER BY tB.cat_id ASC which i believe will show the cat_id 1 and 2 like you need it
There is an error in query... JOIN is missing...
QUERY
SELECT *
FROM tA
INNER JOIN tB
ON tA.id = tB.tA_id
WHERE tB.cat_id = 1
OR tB.cat_id = 2
GROUP BY tA_id
This is fetching expected results
+----+---------------------+-----+--------+--------+----------+
| id | url | id | tA_id | cat_id | cat_name |
+----+---------------------+-----+--------+--------+----------+
| 1 | hxxp://www.aaa.com | 1 | 1 | 1 | mobile |
| 2 | hxxp://www.bbb.com | 3 | 2 | 2 | other |
+----+---------------------+-----+--------+--------+----------+
Try this :-
select *
from tA
inner join tB
on tA.id = tB.tA_id
where tB.cat_id = 1
or tB.cat_id = 2
group by tb.cat_name;
Hope it will help you.
What is the right MySQL query for this? Here's what I want to achieve.
I would like to display all status update entries, date posted, the
poster username, gender, city, and tags
I would like to display all status update entries by a member with user_id='1', the date the status update was posted, the poster username, gender, city, and tags that member has on his profile
And how can I display all the tags that the user have on their profile, in one line separated by ','? I tried group_concat earlier but i couldn't make it work. please help.
Here's my tables
STATUS_UPDATE Table
+------------------------------------------------------+
| status_id | user_id | body | postdate |
+-----------+---------+-----------------+--------------+
| 1 | 1 | hello world | Aug 12, 2012 |
+-----------+---------+-----------------+--------------+
| 2 | 1 | i miss you | Aug 13, 2012 |
+-----------+---------+-----------------+--------------+
| 3 | 2 | lorem ipsum | Aug 14, 2012 |
+-----------+---------+-----------------+--------------+
| 4 | 2 | why me? why? | Aug 14, 2012 |
+-----------+---------+-----------------+--------------+
MEMBERS Table //Primary data of members
+---------------------------------------------------+
| user_id | username | password | email |
+---------+----------+------------+-----------------+
| 1 | john_doe | qwerty | john#doe.com |
+---------+----------+------------+-----------------+
| 2 | maryjane | pass123 | mary#jane.com |
+---------+----------+------------+-----------------+
MEMBERS_DATA Table //Profile Fields
+----------------------------------------------+
| user_id | gender | description | city |
+---------+--------+---------------+-----------+
| 1 | male | i am simple | chicago |
+---------+--------+---------------+-----------+
| 2 | female | i am flirty | newyork |
+---------+--------+---------------+-----------+
MEMBERS_TAGS Table //tags that members added to their profiles
+-------------------+
| user_id | item_id |
+---------+---------+
| 1 | 555 |
+---------+---------+
| 1 | 666 |
+---------+---------+
| 1 | 7777 |
+---------+---------+
| 2 | 8888 |
+---------+---------+
TAGS Table //the info of the tags
+------------------------------+
| item_id | cat_id | name |
+---------+--------+-----------+
| 555 | 5 | sexy |
+---------+--------+-----------+
| 666 | 5 | beauty |
+---------+--------+-----------+
| 7777 | 6 | music |
+---------+--------+-----------+
| 8888 | 6 | movies |
+---------+--------+-----------+
CATEGORY Table //the category of the tags
+-------------------------+
| cat_id | category_name |
+---------+---------------+
| 5 | appearance |
+---------+---------------+
| 6 | hobbies |
+---------+---------------+
BTW I'm coding in PHP, I want to thank you in advance for the help.
SELECT
STATUS_UPDATE.* ,
MEMBERS.username ,
MEMBERS_DATA.gender ,
MEMBERS_DATA.city
TAGS.*
FROM UPDATE
LEFT JOIN MEMBERS ON MEMBERS.user_id = STATUS_UPDATE.user_id
LEFT JOIN MEMBERS_DATA ON MEMBERS_DATA.user_id = STATUS_UPDATE.user_id
LEFT JOIN MEMBERS_TAGS ON MEMBERS_TAGS.user_id = STATUS_UPDATE.user_id
LEFT JOIN TAGS ON TAGS.item_id = MEMBERS_TAGS.item_id