I have one table with unique IDs and second table where these IDs have stored different values. Second table might have several rows with the ID from first table. I want to get print out complete content of table #one and have one more additional column containing only if one of ID matching ID from table one has * (star) character stored.
First Table
|id1|value1|value2|value3|value4|
Second table
|id2|value1|value2|id1|value4|
| | | | | *|
Desired output
|id1|value1|value2|value3|value4|value5 with * or empty
What would be the mysql syntax?
select *, s.value4
from first_table f
left outer join second_table s
on f.id1 = s.id1
and s.value4 = "*"
Related
Let's assume I have those datasets:
Table A:
id (int)
value (varchar)
b_ids(varchar)
1
a value
1
2
another value
2,3
Table B:
id (int)
value (varchar)
1
a value
2
another value
3
another another value
The reason I have to use b_ids here is because the B rows have to be inserted before the A rows
I am trying to SELECT rows from Table A and the corresponding values from Table B in one single query, and make that query a view for filtering purposes
My attemps so far only gave me back the A rows + the first value from the related B rows:
SELECT * FROM A
LEFT JOIN B ON B.id IN (A.b_ids);
And I obtained something like this:
id
value
b_ids
id
value
1
a value
1
1
a value
2
another value
2,3
2
another value
I have tried other joins (INNER JOIN, RIGHT JOIN, CROSS JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN), with the same result
I obviously am still missing something in the joins department if my endeavor is even possible to do in one single SELECT
Is there a way to return the values of Table B as an array of rows in this query?
Even if the result below is the output, I can work with it:
id
value
b_ids
id
value
1
a value
1
1
a value
2
another value
2,3
2
another value
2
another value
2,3
3
another another value
Note: I have chosen Table A as the first table here because the real case involves joins with other tables
you should change tables schema
if there is one to many relationship between A(1)---(n)B
tables should be like this:
Table A:
id (int)
value (varchar)
1
a value
2
another value
Table B:
id (int)
value (varchar)
a_ids(varchar)
1
a value
1
2
another value
2
3
another another value
2
so now you can define tables relationship or fetch data as you need in single query easily
** if tables has many to many relation you need a pivot table.
I need some assistance with deleting data within an SQL Table if it matches data from another table.
There are two Tables
Table 1: DNC
Table 2: Call_Logs
Table 1 has only one column called phone_number.
Table 2 has multiple columns, but the main one that is important is also named phone_number.
Basically, I want to remove any numbers that are in Table 2 from Table 1, if they exist. Now, I don't want to delete every number from Table 1 if they exist in Table 2. What numbers I collect from Table 2 are based on some criteria.
To pull the data from Table 2 that I need to delete from Table 1, I use the following:
select phone_number from call_logs where call_date < 'DATE' and Status = 'DNC'
This query will give me a list of all phone numbers that I would want to remove from Table 1 if it exists.
EXAMPLE:
https://drive.google.com/file/d/0B4NE4ZDXd6steW5odWhBMDJSY1U/view
I am not sure how I would go about running the query in SQL. Any types would be appreciated it.
Looking to your sample in img
You could use a left join on table 2 (where table2.phone_number is null alias don't match)
delete from table1
left join table2 on table1.phone_number = table2.phone_number
where table2.phone_number is null
correlated subquery w/ an exists so it can early exit
The select 1 in the subquery is because we have to select a value but it doesn't matter what value that is. since the coloration (DNC.Phone_Number = CL.Phone_Number) is all we are after; along with your limits on call_log.
DELETE
FROM DNC
WHERE exists (SELECT 1
FROM Call_logs CL
WHERE CL.call_date < 'DATE'
and CL.Status = 'DNC'
and DNC.Phone_Number = CL.Phone_Number)
I have 2 tables with identical column layout but containing different data entered by year. I need to run a query that will find all the entries across both tables where the data in one of the specified columns is the same, such as:
id | serialNo | enteredBy | entryDate| processedBy | processDate ...
rows containing the same serial number (referencing the same item) in both tables.
Try something like:
SELECT * FROM t1 INNER JOIN t2 ON (t1.serialNum = t2.serialNum)
I have two tables roughly designed like this
id | title | price
&
id | title | price | description
I am trying to use a LEFT JOIN to match the results by "id".
This works if I state exactly which table the title should come from i.e a.title. But what I need to do is list the title and price from the second table if it has results. If there are no result from the second table then the first tables results for title a price should be used. When I do any combination of results using select all for both tables it just gives NULL values for the columns that are present in both tables, i.e all but description.
try left join with
coalesce(secondTable.price,firstTable.price) as price,
coalesce(secondTable.title,firstTable.title) as title
I am Using CI with hmvc..
I have 3 tables:
Table 1:
s_id | s_name | s_date
Table 2:
n_id | n_name | n_date
Table 3 contains the relation between table 1 and table 2:
s_id | n_id
I want to fetch values in table 1 or table 2 by ids.
Once I have n_id of table 2, I want to find and to fetch the related values in table 3 and table 1. The related values match by having the same ids.
Please help.
I think this might work for you. I have done something similar in the past while using CI. This is an Active Records query that should be in your model. This query can be shortened but I made it verbose so you see what is going on.
public function get_ids($n1_id){
$this->db->select('*');
$this->db->from('table_3');
$this->db->join('table_2', 'table_2.n1_id = table_3.n1_id');
$this->db->join('table_1', 'table.s1_id = table_3.s1_id');
$this->db->where('n1_id', $n1_id);
$query = $this->db->get();
return $query;
}
Basically once you have one of the id's you can pass it in as a parameter and select all the rows from table 3 that matches the id. Then join table 1 and table 2 on the matching rows in table 3.
Let me know if this works. I have not make the tables to test it.