mysql query for join two table with like - php

company_name | macid | expiry_date
---------------------------------------------
abc | 123456789012 | 2017-03-23
qwe | 987654321012 | 2018-05-24
asd | 456789123012 | 2019-07-07
abc | 789456123000 | 2017-03-23
abc | 445544444444 | 2018-03-03
abc | 555555555555 | 2017-03-25
company_name | desktop | server
abc 123456789012 555555555555
789456123000
I have above two table and I want all macid and expiry date which is present in table1 and table2. Also I have store all macid as new line and desktop macid and server macid in different columns. My query
"select a.macid,a.expity_date from table1 a,table2 b where a.macid like b.desktop or a.macid like %'b.server%'"
but is show result null. Please help to solved.
I want result
macid | expiry_date
---------------------------------------------
123456789012 | 2017-03-23
789456123000 | 2017-03-23
555555555555 | 2017-03-25
for table2 if I want to search mac_id them I have to use
"select * from table2 where desktop like '%123456789012%'"
I can not retrieve record without %(percentage)

I think it is just a typo, not expity_date, it's expiry_date in your query, see demo.
select a.macid, a.expiry_date
from table1 a, table2 b
where a.macid like b.desktop or a.macid like b.server
However, it is more better to use join not a comma(,) to do join things:
select a.macid, a.expiry_date
from table1 a
join table2 b
on a.macid like b.desktop or a.macid like b.server
also check demo here.
Another thing is if desktop and server is as same as macid, just use equal(=) is fine as well.

You have to make two separate JOIN and then UINON the results.
SELECT macid, expiry_date
FROM table1 JOIN table2
ON table1.macid = table2.desktop
UNION
SELECT macid, expiry_date
FROM table1 JOIN table2
ON table1.macid = table2.server
Here is the working demo: http://sqlfiddle.com/#!9/dccea43/1

Try this:
"select a.macid,a.expity_date from table1 a JOIN table2 b ON (a.macid like b.desktop OR a.macid like b.server)"

Related

How to get 3 Users on one ROW using SQL ? (MySQL)

I have two arrays like this,
First Table (infos):
------------------------------
| ID | User1 | User2 | User3 |
------------------------------
| 1 | 20 | 30 | 12 |
------------------------------
Second Table (Users):
---------------------
| ID | Name | Email |
---------------------
| 12 | Test | Test# |
---------------------
| 20 | Bla | Test# |
---------------------
| 30 | Bate | Test# |
---------------------
I want to get the information of users on one row from the IDs on the first table.
I try by getting The row from the first table and fetching on users, but I want to optimize the function with just one Query.
SELECT * FROM infos;
SELECT * FROM Infos i,Users u WHERE u.ID = u.User1 (or 2 ...)
Is there any solution ?
You could use joining the table users 3 times, one for each userid you want show the related name (or other values):
select a.id
, a.user1
, b.Name as user1name
, a.user2
, c.name as user2name
, a.user3
, d.name as user3name
from infos a
inner join Users b on a.user1 = b.id
inner join Users c on a.user1 = c.id
inner join Users d on a.user1 = d.id
And just as suggested, you should not use old implicit join syntax based on comma-separated table names and where clause, you should use (since 1992) explicit joins. This syntax performs the same query, but is more clear.
This is a design error. Use a N:N relation (an additional table) to allow any number of users for the first table. With the relation, other queries will be easier.
A relation table looks like this:
create table relation
(
table1_id int unsigned not NULL,
table2_id int unsigned not NULL,
primary key(table1_id,table2_id)
);
A typical query (and I dislike a.* generally):
select a.*, b.*
from table1 a, table2 b, relation r
where r.table1_id = a.id
&& r.table2_id = b.id

How to join two tables without duplicates?

I have an in table that looks like this:
id_in | date_in
-------|--------------------------
1 | 2016-04-29 02:00:00
And an out table that looks like this:
id_out | date_out
----------|--------------------------
1 | 2016-04-29 03:00:00
2 | 2016-04-29 04:00:00
3 | 2016-04-29 05:00:00
I want to write a query whose output looks like this:
id_in | date_in | id_out | date_out
------|---------------------------- |----------------------------|---------------------------
1 | 2016-04-29 02:00:00 | 1 |2016-04-29 03:00:00
NULL | NULL | 2 |2016-04-29 04:00:00
NULL | NULL | 3 |2016-04-29 05:00:00
You can do this with a left join:
select i.id_in, i.date_in, o.id_out, o.date_out
from outtable o left join
intable i
on o.id_in = i.id_out;
Or you can do this with a right join
select i.id_in, i.date_in, o.id_out, o.date_out
from intable i right join
outtable o
on o.id_in = i.id_out;
Let's call the table with id_in 'table_in' and the table with id_out 'table_out'
You want to join table_in to table_out. In this case, you want to left join table_in to table_out on the id field. This will ensure you return all records from table_out regardless of whether they have a corresponding record in table_in:
select table_in.id_in, table_in.date_in, table_out.id_out, table_out.date_out
from table_out
left join table_in
on table_out.id_out = table_in.id_in
(Alternatively, you can table_in right join table out for the same results)
If you want all the records from both tables regardless of whether there is a corresponding record in the other, you should use full join.
simple you can try this
select * from table_id i right join table_out o on i.id_in = o.id_out
This query results the same as your need..
SELECT a.id_in, a.date_in, b.id_out, b.date_out
FROM intable AS a RIGHT JOIN
outtable AS b
ON a.id_in = b.id_out

MySQL 'Group By' messes up virtual column count

I am trying to select distinct rows within my SQL table, however I'm not having luck in labeling the returned rows appropriately using the code below:
SELECT #row:=#row+1 as rank,
a.id,
a.name
FROM table a,
( SELECT #row:=0) b
GROUP BY a.id
ORDER BY a.name ASC
This query will return the following:
| RANK | ID | NAME
--------------------------
2 | 4483 | Bob
8 | 9453 | Joe
10 | 4543 | Maurice
What I want it to return is this, however:
| RANK | ID | NAME
--------------------------
1 | 4483 | Bob
2 | 9453 | Joe
3 | 4543 | Maurice
Would it be more appropriate for me to use a DISTINCT query for a query of this magnitude?
As per Marc B's solution, I decided to wrap my query with another one however instead I decided to Select DISTINCT columns rather than grouping them which would remove my margin of error, by using this code
SELECT #row:=#row+1 as rank, a.id, a.name FROM
(
SELECT DISTINCT id, name
FROM Table1
) a, (SELECT #row:=0) b
ORDER BY a.name ASC

Selecting unique id in mysql

Below is the replicas of the tables I have created. My goal is to simply pick the unique id_num from the First Table which is not found on the Second Table.
I have tried doing the code below but somehow, I kept getting empty results
SELECT `first_table`.name FROM `first_table`
INNER JOIN `second_table`
ON `first_table`.id_num = `second_table`.id_num
WHERE `first_table`.name = `second_table`.name
First Table:
id_num | name
301 | Bobby
123 | George
25 | Vicky
Second Table:
id_num | name
301 | Bobby
435 | George
25 | Vicky
My desire result I am looking for:
id_num | name
435 | George
LEFT JOIN should work here.
SELECT `first_table`.name FROM `first_table`
LEFT JOIN `second_table`
ON `first_table`.id_num = `second_table`.id_num
WHERE `second_table`.id_num is NULL
See also this useful infographic
try this using NOT IN
select `id_num` , name from `table2` where name not in (
SELECT t1.name FROM `table1` t1
INNER JOIN `table2` t2
ON t1.id_num = t2.id_num )
DEMO HERE

Select from two tables where rows from second table should be shown as columns in first

I have two tables in MySQL:
Table entry:
id | name | date
1 | Test Entry | 12/12/2013
2 | Test Entry 2 | 12/12/2013
Table note
id | entry_id | name | value
1 | 1 | note1 | value1
2 | 1 | note2 | value2
3 | 2 | note1 | value1
4 | 3 | note4 | value4
Where entry_id in note is a foreign key to id in entry.
Is there any solution I can create with a SELECT that will give me a result like the following?
entry_id | name | note1 | note2 | note3
1 | Test Entry | value1 | value2 | -
2 | Test Entry 2 | value 1 | - | value3
I want to avoid LEFT JOIN here (current implementation is working like this) and want to join note only once if that is possible. LEFT JOIN is not good here, because I do not know how many notes can be attached to one entry. My current implementation works that way that I first fetch all distinct notes by name that can be found in note, and then build a SELECT with foreach through PHP. Finally, the SELECT statement looks like this:
SELECT
E.id as entry_id,
E.name as name,
N1.value as note1_value,
N2.value as note2_value,
N3.value as note3_value
FROM entry E
JOIN LEFT note N1 ON E.id = N1.entry_id AND N1.name = 'note1'
JOIN LEFT note N2 ON E.id = N2.entry_id AND N2.name = 'note2'
JOIN LEFT note N3 ON E.id = N3.entry_id AND N3.name = 'note3'
Things get tricky when I join on note 20-30 times.
No, there is not a way to do that without joins.
I would recommend doing 2 queries.
select * from entry where id = id
select * from note where entry_id = id
and then join the results in your application code. You're right, the left joins are going to be bad.
Best would be to use a table with one note value and a note type (number) per each line.
id | value | note_no
Like this you can use as much notes as you like.
You can get the notes on one line using group_concat.
For an example, see here: http://lietoservetruth.wordpress.com/2010/12/13/mysql-group_concat-vertival-concat-concat-records/
This is faster than asking DB twice, and it's better DB design...

Categories