Skip to content
Advertisement

SQL Query not return values

Im running a bit of SQL on my php app, its meant to return a value that gets compared to the value that does get returned.

The query is returning two of the 4 values that i need.

I’ve tried to adapt the query so that it will only return the other two that don’t show up at all

here’s the query

SELECT projects.ProjectProperty1, projects.ProjectProperty2, users.UserProperty1, users.UserProperty2
FROM projects,
     users
WHERE projects.ProjectProperty1 LIKE CONCAT('%', users.UserProperty1, '%')
   OR projects.ProjectProperty2 LIKE CONCAT('%', users.UserProperty2, '%')

Returns: https://gyazo.com/a8a2961166de9c22d9bbf858b3d765b8

Projects DB: https://gyazo.com/8088ea417f0d4804489621948b989068

Users DB: https://gyazo.com/e43e58857a21239e827a59d4864d099a

On Website: https://gyazo.com/2dc7da5855506f512e3737809cd48cee

I need to get the values of the two that are missing out of the users db.

Thanks in advance!

Advertisement

Answer

Try This to get the matched values in projects and users table:

SELECT p.ProjectProperty1, p.ProjectProperty2, u.UserProperty1, u.UserProperty2
FROM projects
Inner Join users u on p.Id= u.Id
WHERE p.ProjectProperty1 LIKE CONCAT('%', u.UserProperty1, '%')
   OR p.ProjectProperty2 LIKE CONCAT('%', u.UserProperty2, '%')

Try this to get all values in Users table and only matched values from Projects table:

 SELECT p.ProjectProperty1, p.ProjectProperty2, u.UserProperty1, u.UserProperty2
    FROM projects
    Right Join users u on p.Id= u.Id
    WHERE p.ProjectProperty1 LIKE CONCAT('%', u.UserProperty1, '%')
       OR p.ProjectProperty2 LIKE CONCAT('%', u.UserProperty2, '%')
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement