i have two tables first table like this
Table 1 name id groups contain 2 field
id_gr
name
Table 2 name Product Contain a lot of field and contain one field name id_gr contain the id_gr Id's from table one i need to replace the Id with the id value
id_dr | name
-----------------
1 | group1
2 | group2
2 | group2
The product table is like this
product_id | name | group
-----------------------------
1 | proudct1 | 1
2 | proudct1 | 2
3 | proudct1 | 3
I need to replace the id in group in product table with its value from group table instead of id.
Try this:
UPDATE table2, table1 SET table2.group = table1.name where table2.group = table1.id
Just for clarification:
table2 is the one with 3 columns
table1 is the one with to columns
Related
I have two column one column associated with another...
Table:base_data
id |---name----|-----des
1 | some name1 | The description1
2 | some name2 | The description2
Table: photos
id |---p_id----|-----photo
1 | 1 | img1s.jpg
2 | 1 | img1w.jpg
3 | 2 | img2.jpg
4 | 2 | img14.jpg
5 | 2 | img15.jpg
I want to select all data from table 1(base_data) and one row from associated row from photos: table how can I do that ????
I don't want to select by greatest n per group I want to select all data from the first table and only one row of the second table which matches with the first table row id, just first match not other.
The Result I want...
id |---name----|---des----|---p_id----|---photo----|
1 | some name |the des..1| 1 | img1s.jpg|
2 | some name |the des..2| 2 | img2.jpg|
I suppose you want to associate base_data with the first photo taken, which should be the one with the lowest photos.id. In MySQL, you could write this as follows: Create an intermediate query which gives - for any p_id - the corresponding record with the lowest id. Then, left join base_data with this intermediate query result. Hope there are not to many typos in it :-) :
select b.id, p2.photo
from base_data b left join
(select p.photo, p.p_id, min(id) from photos p group by p.p_id) p2 on b.id = p2.p_id
If you want the alphanumerically lowest photo name, in MySQL you can do this:
select
t1.*,
t2.photo
from
base_data as t1
left join (
select
p_id,
min(photo) as photo
from
photos
group by
p_id
) as t2 on t2.p_id = t1.id;
I have inherited a table where one column is a comma-separated list of primary keys for a different table:
id | other_ids | value
---|-----------|-------
1 | a,b,c | 100
2 | d,e | 200
3 | f,g | 3000
I would like to convert this table to one where each other_id gets a column of its own:
id | other_id
---|---------
1 | a
1 | b
1 | c
2 | d
2 | e
3 | f
3 | g
However, I cannot think of a way to do this?
The table is > 10 GB in size, so I would like to do this inside the database, if possible.
first time post, please be kind.
Try this
select id,SUBSTRING_INDEX(other_ids,',',1) as other_id from reverseconcat
UNION
select id,SUBSTRING_INDEX(SUBSTRING_INDEX(other_ids,',',2),',',-1) as other_id from reverseconcat
UNION
select id,SUBSTRING_INDEX(SUBSTRING_INDEX(other_ids,',',3),',',-1) as other_id from reverseconcat
order by id
Although I cant really take any credit. Found this on http://www.programering.com/a/MzMyUzNwATg.html
Unsure how you will go on a huge dataset. Also you will need to add more unions if the other_ids are > 3
If you have the other table, then you can use a join and find_in_set():
select t.id, ot.pk as other_id
from t join
othertable ot
on find_in_set(ot.pk, t.other_ids) > 0;
For fast searching table , I need to separate a large table to two tables
example table:
+--------+--------+-------+------+
| source | target | count | prob |
+--------+--------+-------+------+
| test1 | test2 | 2 | 1 |
| cat | dog | 3 | 1.5|
| dog | cat | 1 | 0.5|
+--------+--------+-------+------+
Using the code below
INSERT INTO Table2 (source,target,count,prob)
SELECT source,target,count,prob FROM Table1 WHERE count <2;
then delete originals
DELETE FROM Table1 WHERE count<2;
And count will grouping up after separating table in Table1, and new same element will increase after separating.
For example:
source = 'dog' and target = 'cat' and count = 1 will be move to Table2 and Table1 will still grouping up like add the count or will be add new row source = 'dog' target ='cat' , count = 3.
How could I combine Table1 and Table2 (Table2 will not change after separating)
You can combine the result with UNION
SELECT source, target, count, prob FROM tbl1
UNION
SELECT source, target, count, prob FROM tbl2
Just note there are lots of better ways to get better performance on large tables
Hello I'm having a problem.
I want to search values in the name column using the where clause. Example table is this with values stored.
table1
--------------------------
id | name | anotherid
--------------------------
1 | name1 | 1
2 | name2 | 1
2 | name2 | 2
I have textboxes that I can change how many they appear. For example I have two textboxes. The value on textbox one is 'name1' and the second is 'name2'
My query must find those two values in table1 and, when they're found, their anotherid column must be the same also.
From the first table, All I want to get is this:
table1
--------------------------
id | name | anotherid
--------------------------
1 | name1 | 1
2 | name2 | 1
However when a situation like this appears and I still have two textboxes with the same values:
table1
--------------------------
id | name | anotherid
--------------------------
1 | name1 | 1
2 | name2 | 1
2 | name2 | 2
3 | name3 | 1
The result must be empty.
Now back to the first table. I used this code.
SELECT * FROM `table1` WHERE name = "name1" and name = "name2"
and
SELECT * FROM `table1` WHERE name = "name1" and name = "name2" group by anotherid
but all I get are empty results.
How do you do this?
The problem is in you WHERE clause,
WHERE name = "name1" and name = "name2" will always return false. Use OR instead.
Try any of these:
1. Selecting with name1,name2 and anotherid=1
SELECT *
FROM table1
WHERE (name="name1" OR name="name2")
AND anotherid=1
The result will be:
id name anotherid
1 name1 1
2 name2 1
2. Grouping by anotherid:
SELECT * FROM table1
GROUP BY anotherid
Result:
ID NAME ANOTHERID
1 name1 1
2 name2 2
3. Grouping by name:
SELECT * FROM table1
GROUP BY name
Result:
ID NAME ANOTHERID
1 name1 1
2 name2 1
SELECT * FROM `table1` WHERE name = "name1" OR name = "name2" group by anotherid
That should do.
Try this
SELECT * FROM `table1` group by name
So what I have is something like this:
main_table
id | in_table | in_tables_id | points
1 | t1 | 1 | 1
2 | t2 | 1 | 4
3 | t2 | 2 | 3
4 | t1 | 2 | 2
and then tables
table1 - which in my main_table is marked as t1
id | content_id
1 | 1
2 | 2
table2 - which in my main_table is marked as t2
id | content_id
1 | 1
2 | 2
content
id | category_id | content
1 | 1 | aaa
2 | 2 | bbb
categories
id | name
1 | first
2 | seccond
3 | third
So my first table "main_table" has ids of entries from "table1" and "table2" for which users got their points, those entries are connected to "content" by content_id = id from "content" table; and that content in "content" table is allways in some category from "categories" table.
What I want is to sum all points from "main_table" by categories from "categories" table.
The main problem for me is to somehow "connect" identifiers from "main_table" like t1, t2 with tables "table1", "table2", is there a way to do it?
I recommend restructuring your DB. Have a look at normal forms and transform your tables into normal form, e.g. don't use separate tables, but store the "table name" in a column, so you can do easy joins.
For your current table layout use something like the following join conditions:
select *
from main_table mt
inner join table1 t1
on mt.in_table = 't1'
and mt.in_tables_id = t1.id
inner join table2 t2
on mt.in_table = 't2'
and mt.in_tables_id = t2.id
/* repeat for number of tables */