Lets say I have a table with the following rows/values:
+--------+----------+
| ID | adspot |
+--------+----------+
| 1 | A |
| 2 | B |
| 3 | A |
| 4 | B |
| 5 | C |
| 6 | A |
+--------+----------+
I need a way to select the values in adspot but only once if they're duplicated. So from this example I'd want to select A once and B once. The SQL result should look like this then:
+----------+
| adspot |
+----------+
| A |
| B |
| C |
+----------+
I'm using mySQL and PHP, in case anyone asks.
Thanks.
SELECT DISTINCT adspot FROM your_table; ( this may not perform well at all in large tables )
SELECT adspot FROM table GROUP BY adspot
see: http://www.tizag.com/mysqlTutorial/mysqlgroupby.php
Related
I need to combine information from two tables into one table, the following table is
table 1
+----+---------------+---------+
|id_k| name | value |
+----+---------------+---------+
| 1 | enak | 4 |
| 2 | nor | 3 |
+----+---------------+---------+
table 2
+------+------+---------+
| id_d | id_k | feel |
+------+------+---------+
| 1 | 1 | good |
| 2 | 1 | better |
| 3 | 1 | verygood|
+------+------+---------+
result should be
+------+------+-------+------------------------+
| id_d | name | value | feel |
+------+------+-------+------------------------+
| 1 | enak | 4 | good, better, verygood |
| 2 | nor | 3 | |
+------+------+-------+------------------------+
this is my code [not worked]
select k.name, k.value, s.feel
from table1 as k
left join table2 as s on s.id_k=k.id_k
http://sqlfiddle.com/#!9/3a7564/1
SELECT t1.id_k,
t1.`name`,
t1.`value`,
GROUP_CONCAT(t2.feel) AS feel
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id_k = t2.id_k
GROUP BY t1.id_k
You could use the gorup_concat function to concatinate the values from table2 to a coma-delimited string in the result:
SELECT table1.id_k, name, value, GROUP_CONCAT(feel SEPARATOR ', ') AS feel
FROM table1
JOIN table2 ON table1.id_k = table2.id_k
GROUP BY table1.id_k
I'm fairly new to sql. This might be basic. I have two tables one with groups and one with members, I want to link them up so that a third table contains id_group and id_member. The value MYGROUP is supplied during the import. I tried this:
insert ignore into member_group (id_group, id_member)
values ( ( select id_group from group where group_name='MYGROUP' ) ,
( select id_member from member ) );
But I end up with one row in member_group containing a null value.
on it's own this yields 1 for example:
select id_group from group where group_name='MYGROUP';
+----------+
| id_group |
+----------+
| 1 |
+----------+
on it's own this yields a list of id_members
mysql> select id_member from member;
+-----------+
| id_member |
+-----------+
| 123 |
| 456 |
| 789 |
I want member_group to then look like this
+-----------+----------+
| id_group |id_member |
+-----------+----------+
| 1 | 123 |
| 1 | 456 |
| 1 | 789 |
How can I do this (without resorting to shell scripts, for loops and sed) ?
As requested,
mysql> select * from group;
+----------+------------------+
| id_group | group_name |
+----------+------------------+
| 1 | vip-member |
| 2 | standard-member |
mysql> select * from member;
+-----------+----------+
| id_member | fullname |
+-----------+----------+
| 123 | Bob |
| 456 | Pete |
Which, if I could get it working, should look like below.
mysql> select * from member_group;
+------------------+----------+-----------+
| id_member_groups | id_group | id_member |
+------------------+----------+-----------+
| 1 | 1 | 123 |
| 2 | 1 | 456 |
| 3 | 1 | 789 |
| 4 | 2 | 123 |
| 5 | 2 | 789 |
id_group is supplied during the import phase. One batch of say 200 members, will be members of the same id_group. I was thinking about adding the group_id to a temporary table. But I'm a tad lost to be honest.
You can do a cross join to achieve your goal.
insert ignore into member_group (id_group, id_member)
select member_group.id_group, member.id_member
from member_group
cross join member
where group_name = 'MYGROUP';
If I insert a value with an order by name like this.
+------------------+
| Table: example |
+------------------+
| Name | Value |
+---------+--------+
| A | 153 |
| B | 10 |
| C | 20 |
| D | 50 |
| E | 100 |
+---------+--------+
When I select the value with a query
select * from example
or
select * from example limit 2
without a where, order by or group by, is the order of the result defaulted to the insert order??
Thanks,
I have two tables testa and testb.
mysql> select * from testa;
+------+-------+
| id | dcac |
+------+-------+
| 1 | hello |
| 2 | world |
+------+-------+
2 rows in set (0.00 sec)
mysql> select * from testb;
+------+------+
| a_id | x |
+------+------+
| 1 | a |
| 1 | b |
| 1 | b |
| 1 | c |
| 2 | x |
+------+------+
5 rows in set (0.00 sec)
How do I add a column (x_list) to testa such that it's the comma-seperated list of xs from testb wherever testa.id is testb.a_id
So the output I'm expecting is somewhat like this -
+------+-------+--------+
| id | dcac | x-list |
+------+-------+--------+
| 1 | hello | a,b,b,c|
| 2 | world | x |
+------+-------+--------+
I tried using some complex join statements and I looked at this http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat (did not really understand much)
But I'm not able to proceed. What do I do?
Thanks.
Try:
SELECT testa.id, testa.dcac, GROUP_CONCAT(testb.x)
FROM testb
INNER JOIN testa ON testb.a_id = testa.id
GROUP BY testb.a_id
You can see result here: http://sqlfiddle.com/#!2/28887/2/0
I need to make a mySQL query and am not sure what the format should be.
Here is the situation, I have a table with the fields - id, name, type
I would use a query similar to the following to get results from the table:
SELECT * FROM table WHERE type='1'
However, I have a list of ID's from another query. These are items that should be excluded from the results.
I'm sure the answer is simple, but I don't know enough about mySQL queries to find the answer.
A simple NOT IN will be what you want :) It lets you send a list of values and makes sure that its not in them :)
SELECT * FROM table WHERE ID NOT IN (1,2,3)
You could also do it with a subquery with something like:
SELECT id FROM table WHERE ID NOT IN (SELECT id FROM table2 WHERE type = 1)
You can use NOT IN query like this:
SELECT * FROM `table` WHERE `type`=1 WHERE `id` NOT IN (SELECT `blocked_ids` FROM `block`);
So, you would be having blocked IDs in the block table! Hope this helps! :)
Consider this table:
+----+-------------+
| id | name |
+----+-------------+
| 1 | America |
| 2 | Europe |
| 3 | India |
| 4 | Japan |
| 5 | Brazil |
| 6 | Switzerland |
| 7 | Syria |
| 8 | Wales |
| 9 | Taiwan |
| 10 | Zaire |
+----+-------------+
And the blocked table:
+-----+
| IDs |
+-----+
| 1 |
| 4 |
| 6 |
| 8 |
| 9 |
+-----+
Now, when I give a query like:
SELECT * FROM `countries` WHERE `id` NOT IN (SELECT * FROM `blocked`);
I get this result:
+----+--------+
| id | name |
+----+--------+
| 2 | Europe |
| 3 | India |
| 5 | Brazil |
| 7 | Syria |
| 10 | Zaire |
+----+--------+
Hope this helps! :)