my data there will be a number key how to equalize with another table so it can be called names. i want to changes data (number key) to text from other table.
my table1 :
+-----------------------+
| ID | Name | Category |
-------------------------
| 1 | Home | 21 |
| 2 | Pro | 23 |
+-----------------------+
and table 2 :
+---------------------+
| ID | name_category |
-----------------------
| 21 | Sweet Home |
| 23 | your Home |
+---------------------+
how to get the same ID?. but the data will be shown in table 1
Try like this.Use join on table1.ID = table2.ID.displays all data of table1 with category names from table2 matching on table1 category ID.
$this->db->select(*)
->from('table1')
->join('table2', 'table1.ID = table2.ID');
$result = $this->db->get()->result_array();
print_r($result);//displays all data of table1 with category names from table2 matching on table1 category ID.
$this->db->query('select table1.* from table1 inner join table2 on table1.id = table2.id');
$this->db->result();
Related
I work with PHP and PDO.
So I have 2 tables like,
Table 1
| id | name | age |
| 1 | John | 25 |
| 2 | Tom | 32 |
| 3 | James| 45 |
Table 2
| id | Comment | Link |
| 1 | some text | 3 |
| 2 | some text | 3 |
| 3 | some text | 1 |
So, Link column numbers represent id's in table1. For example Link = 3s in table 2 represent James in table 1. I need a query which brings all table1's data and also a number of repeated value for related Link column which comes from table2.
For example, the query should give me (let's choose James),
| id | name | age | Value |
| 3 | James | 45 | 2 |
value=2, because there are two 3s in link column which related to James
I tried somethings but got lots of errors.
I think you just need the GROUP BY
SELECT a.id,
a.name,
a.age,
count(*) as value
FROM table1 a
JOIN table2 b ON a.id = b.link
GROUP BY a.id, a.name, a.age
If you really want just one row then add WHERE
SELECT a.id,
a.name,
a.age,
count(*) as value
FROM table1 a
JOIN table2 b ON a.id = b.link
WHERE a.name = 'James'
GROUP BY a.id, a.name, a.age
or use subquery
SELECT a.id,
a.name,
a.age,
(SELECT count(*) FROM table2 b WHERE a.id = b.link) as value
FROM table1 a
WHERE a.name = 'James'
My two table setup is like below:
table1
+------+---------+--------------------------------------+
| id | tail | content |
+------+---------+--------------------------------------+
| 1 | abc | ... |
| 2 | def | ... |
| 3 | ghi | ... |
| 4 | def | ... |
| 5 | jkl | ... |
+------+-------+----------------------------------------+
table2
+------+--------+---------------------------------------+
| id | tailID | value | others |
+------+--------+---------------------------------------+
| 1 | 2 | 412 | |
| 2 | 3 | 215 | |
| 1 | 2 | 571 | |
| 1 | 4 | 123 | |
+------+--------+---------------------------------------+
I like to get all columns from this two tables in a row with matched tail = tailID but not duplicate rows which has same tail.
For the duplicate TAIL, just need to get the single row of max VALUE of same tail.
I am currently using
SELECT table1.tail, table2.other_column
FROM table1
INNER JOIN table2
on table1.id = table2.tailID
WHERE table1.some_coloum = "a sepecific string"
ORDER BY table2.value
But it returns many duplicates of same tail.
I just need to have single row for duplicate TAIL with hightes VALUE of table2.
DISTINCT with CROSS APPLY:
SELECT DISTINCT t1.tail,
t2.other_column,
t3.[value]
FROM table1 t1
CROSS APPLY (
SELECT tailid,
MAX([value]) as [value]
FROM table2
WHERE tailid = t1.id
GROUP BY tailid
) as t3
INNER JOIN table2 t2
ON t2.tailid = t3.tailid AND t3.[value] = t2.[value]
WHERE t1.some_coloum = "a sepecific string"
First group table2 then join
SELECT table1.tail, table2.other_column
FROM table1
INNER JOIN (
SELECT tailID, max(value) as value
FROM table2
GROUP BY tailID
) t2g ON t2g.tailID = table1.ID
INNER JOIN table2
on t2g.tailID = table2.tailID AND t2g.value = table2.value
WHERE table1.some_coloum = "a sepecific string"
ORDER BY table2.value
The query still may return multiple rows for a table1 row if there are 2 or more rows in table2 with the same max(value) and tailID.
Selected only rows where is MAX value of column value
SELECT table1.tail, MAX(table2.value)
FROM
table1
INNER JOIN table2 ON table1.id = table2.tailID
WHERE table1.content = "test"
http://sqlfiddle.com/#!9/b70d29/3/0
Hi I'm trying to copy rows in a table but I need it with a different id, that can be in that table aswell. Both id and second_id are primary key. and foreign keys.
+----+-----------+
| id | second_id |
+----+-----------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 2 |
+----+-----------+
So I need to copy all of second_id of id 1 to the id 2, but and eventually, if there's going to be id 3, copy that aswell.
the result should be
+----+-----------+
| id | second_id |
+----+-----------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 2 |
| 2 | 1 |
| 2 | 3 |
+----+-----------+
Also id is a foreign key, so if I have id 3 copy it aswell just like id 2
Any solutions ?
Use a self-join of the table to get all the combinations of id and second_id. Then use a LEFT JOIN to filter out the combinations that already exist, so you can insert the rest into the table.
INSERT INTO yourTable (id, second_id)
SELECT DISTINCT t1.id, t2.second_id
FROM yourTable AS t1
CROSS JOIN yourTable AS t2
LEFT JOIN yourTable AS t3 ON t1.id = t3.id AND t2.second_id = t3.second_id
WHERE t3.id IS NULL
DEMO
Instead of the LEFT JOIN you could simply use INSERT IGNORE. Since (id, second_id) is the primary key, the duplicates will simply be ignored when inserting.
INSERT IGNORE INTO yourTable (id, second_id)
SELECT DISTINCT t1.id, t2.second_id
FROM yourTable AS t1
CROSS JOIN yourTable AS t2
DEMO
I have a problem. I have 2 database tables.
table 1 people:
+----------+--------------+
| id | name |
+----------+--------------+
| 1 | johanalj |
| 2 | hjgjhggjh |
+----------+--------------+
table 2 images of people:
+----------+--------------+----------------+
| id | url | people_ID |
+----------+--------------+----------------+
| 1 | 3765345.png | 1 |
| 2 | 87e58974.png | 1 |
+----------+--------------+----------------+
Now I want to select person with id 1 from table 1 and all pictures from table 2 that have people_ID 1.
I tried LEFT JOIN in combination with a WHERE but cant get it to work
$sql = "SELECT * FROM people p LEFT JOIN images i ON i.people_ID = p.id WHERE id = '1'";
But I get a no result massage. What am I doing wrong?
There is an error(ambiguous column id). Both tables have id column. You need to add the table alias with id. try with -
$sql = "SELECT * FROM people p LEFT JOIN images i ON i.people_ID = p.id WHERE p.id = '1'";
I have four tables in MySQL.
+-----------------------------------------------+
Table 2 | Table 1 |
+============+=========+========+============+ +==========+=====================+
| ID | Name | Add | d_id | | ID | Details |
+============+=========+========+============+ +==========+=====================+
| 1 | ABC | City | 3 | | 1 | blah blah blah |
+------------+---------+--------+------------+ +----------+---------------------+
: : : : : : : :
|
+-------------------------+
|
Table 3 | Table 4
+============+==========+============+ +=========+======================+
| d_id | dis_Name | cat_id |----+ | cat_id | category_name |
+============+==========+============+ | +=========+======================+
| 1 | Myeloma | 5 | | : : :
+------------+----------+------------+ | +---------+----------------------+
: : : : +----| 5 | Cancer |
+---------+----------------------+
What I want to do is, I want to select all the rows in 'Table 1' those belongs to the 'category_name' in 'Table 4'.
Table 2 may contain multiple rows having same 'd_id'. Likewise, Table 3 may have multiple rows having same 'cat_id'.
If I select Category 'Cancer' then I would be getting multiple d_id's from table 3 in result. For each d_id, there will be multiple ID's in table 2. I want to fetch the details of these ID's (from Table 2) from Table 1. What would be the query statement???
Connectivity is shown in the table representaion.
you can achieve it doing this:
SELECT t1.*
FROM t4
INNER JOIN t3 ON (t4.cat_id = t3.cat_id)
INNER JOIN t2 ON (t3.d_id = t2.d_id)
INNER JOIN t1 ON (t2.id = t1.id)
WHERE t4.category_name = 'Cancer';
What you need is a join.
SELECT table.column_in_table
FROM table
INNER JOIN table_with_same_values
ON table.column_in_table = table_with_same_values.column_in_that_table;
I'm still not clear what you're trying to ask. Your question is a bit verbose. Some additional info here.
Hope this helps!